Skip to content

Commit

Permalink
feat: add launchdarkly provider (GoogleCloudPlatform#1133)
Browse files Browse the repository at this point in the history
update doc

Signed-off-by: Rui Chen <rui@chenrui.dev>
  • Loading branch information
chenrui333 authored Dec 4, 2021
1 parent bb010c9 commit 3bfcfcb
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ A CLI tool that generates `tf`/`json` and `tfstate` files based on existing infr
* [Equinix Metal](/docs/equinixmetal.md)
* [Fastly](/docs/fastly.md)
* [Heroku](/docs/heroku.md)
* [LaunchDarkly](/docs/launchdarkly.md)
* [Linode](/docs/linode.md)
* [NS1](/docs/ns1.md)
* [OpenStack](/docs/openstack.md)
Expand Down Expand Up @@ -205,7 +206,7 @@ It's possible to combine `--compact` `--path-pattern` parameters together.
From source:
1. Run `git clone <terraformer repo> && cd terraformer/`
2. Run `go mod download`
3. Run `go build -v` for all providers OR build with one provider
3. Run `go build -v` for all providers OR build with one provider
`go run build/main.go {google,aws,azure,kubernetes,etc}`
4. Run ```terraform init``` against a ```versions.tf``` file to install the plugins required for your platform. For example, if you need plugins for the google provider, ```versions.tf``` should contain:

Expand Down Expand Up @@ -260,6 +261,7 @@ Links to download Terraform Providers:
* Cloud
* DigitalOcean provider >1.9.1 - [here](https://releases.hashicorp.com/terraform-provider-digitalocean/)
* Heroku provider >2.2.1 - [here](https://releases.hashicorp.com/terraform-provider-heroku/)
* LaunchDarkly provider >=2.1.1 - [here](https://releases.hashicorp.com/terraform-provider-launchdarkly/)
* Linode provider >1.8.0 - [here](https://releases.hashicorp.com/terraform-provider-linode/)
* OpenStack provider >1.21.1 - [here](https://releases.hashicorp.com/terraform-provider-openstack/)
* TencentCloud provider >1.50.0 - [here](https://releases.hashicorp.com/terraform-provider-tencentcloud/)
Expand Down
45 changes: 45 additions & 0 deletions cmd/provider_cmd_launchdarkly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 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 (
launchdarkly_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/launchdarkly"

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

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

cmd.AddCommand(listCmd(newLaunchDarklyProvider()))
baseProviderFlags(cmd.PersistentFlags(), &options, "project", "launchdarkly_project=id1:id2:id3")
return cmd
}

func newLaunchDarklyProvider() terraformutils.ProviderGenerator {
return &launchdarkly_terraforming.LaunchDarklyProvider{}
}
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
newCmdDigitalOceanImporter,
newCmdEquinixMetalImporter,
newCmdHerokuImporter,
newCmdLaunchDarklyImporter,
newCmdLinodeImporter,
newCmdOpenStackImporter,
newCmdTencentCloudImporter,
Expand Down Expand Up @@ -99,6 +100,7 @@ func providerGenerators() map[string]func() terraformutils.ProviderGenerator {
newEquinixMetalProvider,
newFastlyProvider,
newHerokuProvider,
newLaunchDarklyProvider,
newLinodeProvider,
newNs1Provider,
newOpenStackProvider,
Expand Down
13 changes: 13 additions & 0 deletions docs/launchdarkly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Use with Launchdarkly

Example:

```
export LAUNCHDARKLY_ACCESS_TOKEN=[LAUNCHDARKLY_ACCESS_TOKEN]
./terraformer import launchdarkly -r project
```

List of supported LaunchDarkly resources:

* `project`
* `launchdarkly_project`
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ require (
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/antihax/optional v1.0.0 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v12 v12.0.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
Expand Down Expand Up @@ -268,6 +269,7 @@ require (
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/klauspost/compress v1.11.2 // indirect
github.com/launchdarkly/api-client-go v5.3.0+incompatible
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattermost/xml-roundtrip-validator v0.0.0-20201213122252-bcd7e1b9601e // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ github.com/andybalholm/crlf v0.0.0-20171020200849-670099aa064f/go.mod h1:k8feO4+
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/antchfx/xpath v0.0.0-20190129040759-c8489ed3251e/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk=
github.com/antchfx/xquery v0.0.0-20180515051857-ad5b8c7a47b0/go.mod h1:LzD22aAzDP8/dyiCKFp31He4m2GPjl0AFyzDtZzUu9M=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/openwhisk-client-go v0.0.0-20210106144548-17d556327cd3 h1:CMvrWrV6C3FOAscQwvCcRGQyQ93KLMTUXCFFS+JGgP4=
github.com/apache/openwhisk-client-go v0.0.0-20210106144548-17d556327cd3/go.mod h1:jLLKYP7+1+LFlIJW1n9U1gqeveLM1HIwa4ZHNOFxjPw=
github.com/apparentlymart/go-cidr v1.0.1/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
Expand Down Expand Up @@ -902,6 +904,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labd/commercetools-go-sdk v0.3.1 h1:Fk8/VKKDSNHhyVhDywJdmzUt3JSpNoFZbHJndwjVBls=
github.com/labd/commercetools-go-sdk v0.3.1/go.mod h1:I+KKNALlg6PcSertsVA7E442koO99GT7gldWqwZlUGo=
github.com/launchdarkly/api-client-go v5.3.0+incompatible h1:xB4QGNbNzAUm2UDdtlsHhmCrs6l7OQGWFgs1xB6s/u8=
github.com/launchdarkly/api-client-go v5.3.0+incompatible/go.mod h1:INGa7NUZYSwVozwPV7l6ikgD7pzSOpZvg9I5sqCZIWs=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
Expand Down
81 changes: 81 additions & 0 deletions providers/launchdarkly/launchdarkly_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2020 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 launchdarkly

import (
"errors"
"os"

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

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

const (
basePath = "https://app.launchdarkly.com/api/v2"
version = "0.0.1"
APIVersion = "20191212"
)

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

return nil
}

func (p *LaunchDarklyProvider) GetName() string {
return "launchdarkly"
}

func (p *LaunchDarklyProvider) GetProviderData(arg ...string) map[string]interface{} {
return map[string]interface{}{
"provider": map[string]interface{}{
"launchdarkly": map[string]interface{}{
"api_key": p.apiKey,
},
},
}
}

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

func (p *LaunchDarklyProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
return map[string]terraformutils.ServiceGenerator{
"project": &ProjectGenerator{},
}
}

func (p *LaunchDarklyProvider) InitService(serviceName string, verbose bool) error {
var isSupported bool
if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
return errors.New("launchdarkly: " + 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/launchdarkly/launchdarkly_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020 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 launchdarkly

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

type LaunchDarklyService struct { //nolint
terraformutils.Service
}
67 changes: 67 additions & 0 deletions providers/launchdarkly/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2021 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 launchdarkly

import (
"context"
"fmt"
"os"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
launchdarkly "github.com/launchdarkly/api-client-go"
)

type ProjectGenerator struct {
LaunchDarklyService
}

func (g *ProjectGenerator) loadProjects(ctx context.Context, client *launchdarkly.APIClient) error {
projects, _, err := client.ProjectsApi.GetProjects(ctx)
if err != nil {
return err
}

for _, project := range projects.Items {
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
project.Key,
project.Key,
"launchdarkly_project",
"launchdarkly",
[]string{}))
}
return nil
}

func (g *ProjectGenerator) InitResources() error {
apiKey := os.Getenv("LAUNCHDARKLY_ACCESS_TOKEN")
cfg := &launchdarkly.Configuration{
BasePath: basePath,
DefaultHeader: make(map[string]string),
UserAgent: fmt.Sprintf("launchdarkly-terraform-provider/%s", version),
}
cfg.AddDefaultHeader("LD-API-Version", APIVersion)

client := launchdarkly.NewAPIClient(cfg)

ctx := context.WithValue(context.Background(), launchdarkly.ContextAPIKey, launchdarkly.APIKey{
Key: apiKey,
})

if err := g.loadProjects(ctx, client); err != nil {
return err
}

return nil
}

0 comments on commit 3bfcfcb

Please sign in to comment.