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.
feat: add launchdarkly provider (GoogleCloudPlatform#1133)
update doc Signed-off-by: Rui Chen <rui@chenrui.dev>
- Loading branch information
1 parent
bb010c9
commit 3bfcfcb
Showing
9 changed files
with
240 additions
and
1 deletion.
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
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 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{} | ||
} |
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,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` |
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,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 | ||
} |
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 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 | ||
} |
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,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 | ||
} |