forked from GoogleCloudPlatform/terraformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ns1: init provider and add zone support
- Loading branch information
1 parent
476fac8
commit 5221aa4
Showing
7 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |