Skip to content

Commit

Permalink
ns1: init provider and add zone support
Browse files Browse the repository at this point in the history
  • Loading branch information
chenrui333 committed Jun 7, 2020
1 parent 476fac8 commit 5221aa4
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 0 deletions.
45 changes: 45 additions & 0 deletions cmd/provider_cmd_ns1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd

import (
ns1_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/ns1"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/spf13/cobra"
)

func newCmdNs1Importer(options ImportOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "ns1",
Short: "Import current state to Terraform configuration from NS1",
Long: "Import current state to Terraform configuration from NS1",
RunE: func(cmd *cobra.Command, args []string) error {
provider := newNs1Provider()
err := Import(provider, options, []string{})
if err != nil {
return err
}
return nil
},
}

cmd.AddCommand(listCmd(newNs1Provider()))
baseProviderFlags(cmd.PersistentFlags(), &options, "zone", "zone=id1:id2:id4")
return cmd
}

func newNs1Provider() terraformutils.ProviderGenerator {
return &ns1_terraforming.Ns1Provider{}
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func providerImporterSubcommands() []func(options ImportOptions) *cobra.Command
newCmdFastlyImporter,
newCmdHerokuImporter,
newCmdLinodeImporter,
newCmdNs1Importer,
newCmdOpenStackImporter,
newCmdVultrImporter,
// Infrastructure Software
Expand Down Expand Up @@ -83,6 +84,7 @@ func providerGenerators() map[string]func() terraformutils.ProviderGenerator {
newFastlyProvider,
newHerokuProvider,
newLinodeProvider,
newNs1Provider,
newOpenStackProvider,
newVultrProvider,
// Infrastructure Software
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/labd/commercetools-go-sdk v0.0.0-20200309143931-ca72e918a79d
github.com/linode/linodego v0.15.0
github.com/mrparkers/terraform-provider-keycloak v0.0.0-20200506151941-509881368409
github.com/ns1/ns1-go v2.4.0+incompatible
github.com/paultyng/go-newrelic/v4 v4.10.0
github.com/pkg/errors v0.9.1
github.com/smartystreets/goconvey v1.6.4 // indirect
Expand All @@ -48,6 +49,7 @@ require (
google.golang.org/api v0.22.0
google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84
gopkg.in/jarcoal/httpmock.v1 v1.0.0-00010101000000-000000000000 // indirect
gopkg.in/ns1/ns1-go.v2 v2.4.0 // indirect
k8s.io/apimachinery v0.17.5
k8s.io/client-go v0.17.5
k8s.io/utils v0.0.0-20191218082557-f07c713de883 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/nicksnyder/go-i18n v1.10.1/go.mod h1:e4Di5xjP9oTVrC6y3C7C0HoSYXjSbhh/dU0eUV32nB4=
github.com/ns1/ns1-go v2.4.0+incompatible h1:WYLNc1preJKfVVJL0zCOXfePlREOwrpJrDhJNn1nnLI=
github.com/ns1/ns1-go v2.4.0+incompatible/go.mod h1:+5cGIDXMoO+J3+C8FJ8J0xkyiTdgCvDA+JXQ7f1cPKs=
github.com/nsf/gocode v0.0.0-20190302080247-5bee97b48836/go.mod h1:6Q8/OMaaKAgTX7/jt2bOXVDrm1eJhoNd+iwzghR7jvs=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
Expand Down Expand Up @@ -917,6 +919,8 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk=
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ns1/ns1-go.v2 v2.4.0 h1:/E339/pkpDYfaDqW4+sBdE2r4UmqzNuqWCeIgtJZnlE=
gopkg.in/ns1/ns1-go.v2 v2.4.0/go.mod h1:VV+3haRsgDiVLxyifmMBrBIuCWFBPYKbRssXB9z67Hw=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
Expand Down
76 changes: 76 additions & 0 deletions providers/ns1/ns1_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ns1

import (
"errors"
"os"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/GoogleCloudPlatform/terraformer/terraformutils/providerwrapper"
)

type Ns1Provider struct { //nolint
terraformutils.Provider
apiKey string
}

func (p *Ns1Provider) Init(args []string) error {
if os.Getenv("NS1_APIKEY") == "" {
return errors.New("set NS1_APIKEY env var")
}
p.apiKey = os.Getenv("NS1_APIKEY")

return nil
}

func (p *Ns1Provider) GetName() string {
return "ns1"
}

func (p *Ns1Provider) GetProviderData(arg ...string) map[string]interface{} {
return map[string]interface{}{
"provider": map[string]interface{}{
"ns1": map[string]interface{}{
"version": providerwrapper.GetProviderVersion(p.GetName()),
},
},
}
}

func (Ns1Provider) GetResourceConnections() map[string]map[string][]string {
return map[string]map[string][]string{}
}

func (p *Ns1Provider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
return map[string]terraformutils.ServiceGenerator{
"zone": &ZoneGenerator{},
}
}

func (p *Ns1Provider) InitService(serviceName string, verbose bool) error {
var isSupported bool
if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
return errors.New("ns1: " + serviceName + " not supported service")
}
p.Service = p.GetSupportedService()[serviceName]
p.Service.SetName(serviceName)
p.Service.SetVerbose(verbose)
p.Service.SetProviderName(p.GetName())
p.Service.SetArgs(map[string]interface{}{
"api_key": p.apiKey,
})
return nil
}
23 changes: 23 additions & 0 deletions providers/ns1/ns1_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ns1

import (
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

type Ns1Service struct { //nolint
terraformutils.Service
}
52 changes: 52 additions & 0 deletions providers/ns1/zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2019 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ns1

import (
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
ns1 "github.com/ns1/ns1-go"
)

type ZoneGenerator struct {
Ns1Service
}

func (g *ZoneGenerator) createZoneResources(client *ns1.APIClient) error {
zones, err := client.GetZones()
if err != nil {
return err
}

for _, zone := range zones {
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
zone.Id,
zone.Id,
"ns1_zone",
"ns1",
[]string{}))
}

return nil
}

func (g *ZoneGenerator) InitResources() error {
client := ns1.New(g.Args["api_key"].(string))

if err := g.createZoneResources(client); err != nil {
return err
}

return nil
}

0 comments on commit 5221aa4

Please sign in to comment.