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.
azuredevops: new provider (GoogleCloudPlatform#1054)
* azuredevops: scafolding * azuredevops: fixes for a minimal working version * azuredevops: get GetResourceConnections per service * azuredevops: added azuredevops * azuredevops: simplified services impl * azuredevops: added group * azuredevops: added docs * azuredevops: fix ci lints * azuredevops: silence critic
- Loading branch information
Showing
11 changed files
with
425 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 ( | ||
azuredevops "github.com/GoogleCloudPlatform/terraformer/providers/azuredevops" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCmdAzureDevOpsImporter(options ImportOptions) *cobra.Command { | ||
|
||
cmd := &cobra.Command{ | ||
Use: "azuredevops", | ||
Short: "Import current state to Terraform configuration from Azure DevOps", | ||
Long: "Import current state to Terraform configuration from Azure DevOps", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
provider := newAzureDevOpsProvider() | ||
err := Import(provider, options, []string{options.ResourceGroup}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.AddCommand(listCmd(newAzureDevOpsProvider())) | ||
baseProviderFlags(cmd.PersistentFlags(), &options, "project,team,git", "project=name1:name2:name3") | ||
return cmd | ||
} | ||
|
||
func newAzureDevOpsProvider() terraformutils.ProviderGenerator { | ||
return &azuredevops.AzureDevOpsProvider{} | ||
} |
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,26 @@ | ||
# Use with Azure DevOps | ||
|
||
Supports acess via [Personal Access Token](https://registry.terraform.io/providers/microsoft/azuredevops/latest/docs/guides/authenticating_using_the_personal_access_token). | ||
|
||
## Example | ||
|
||
``` sh | ||
export AZDO_ORG_SERVICE_URL="https://dev.azure.com/<Your Org Name>" | ||
export AZDO_PERSONAL_ACCESS_TOKEN="<Personal Access Token>" | ||
|
||
./terraformer import azuredevops -r * | ||
./terraformer import azuredevops -r project,git_repository | ||
``` | ||
|
||
## List of supported Azure resources | ||
|
||
* `project` | ||
* `azuredevops_project` | ||
* `group` | ||
* `azuredevops_group` | ||
* `git_repository` | ||
* `azuredevops_git_repository` | ||
|
||
## Notes | ||
|
||
Since [Terraform Provider for Azure DevOps](https://github.com/microsoft/terraform-provider-azuredevops) `version 0.17`. |
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,96 @@ | ||
// 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 azuredevpos | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
) | ||
|
||
type AzureDevOpsProvider struct { //nolint | ||
terraformutils.Provider | ||
organizationURL string | ||
personalAccessToken string | ||
} | ||
|
||
func (p *AzureDevOpsProvider) setEnvConfig() error { | ||
|
||
organizationURL := os.Getenv("AZDO_ORG_SERVICE_URL") | ||
if organizationURL == "" { | ||
return errors.New("environment variable AZDO_ORG_SERVICE_URL missing") | ||
} | ||
personalAccessToken := os.Getenv("AZDO_PERSONAL_ACCESS_TOKEN") | ||
if personalAccessToken == "" { | ||
return errors.New("environment variable AZDO_PERSONAL_ACCESS_TOKEN missing") | ||
} | ||
p.organizationURL = organizationURL | ||
p.personalAccessToken = personalAccessToken | ||
return nil | ||
} | ||
|
||
func (p *AzureDevOpsProvider) Init(args []string) error { | ||
err := p.setEnvConfig() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (p *AzureDevOpsProvider) GetName() string { | ||
return "azuredevops" | ||
} | ||
|
||
func (p *AzureDevOpsProvider) GetProviderData(arg ...string) map[string]interface{} { | ||
return map[string]interface{}{} | ||
} | ||
|
||
func (p AzureDevOpsProvider) GetResourceConnections() map[string]map[string][]string { | ||
supported := p.GetSupportedService() | ||
connections := make(map[string]map[string][]string) | ||
for serviceName, service := range supported { | ||
if service2, ok := service.(AzureDevOpsServiceGenerator); ok { | ||
if conn := service2.GetResourceConnections(); conn != nil { | ||
connections[serviceName] = conn | ||
} | ||
} | ||
} | ||
return connections | ||
} | ||
|
||
func (p *AzureDevOpsProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator { | ||
return map[string]terraformutils.ServiceGenerator{ | ||
"project": &ProjectGenerator{}, | ||
"group": &GroupGenerator{}, | ||
"git_repository": &GitRepositoryGenerator{}, | ||
} | ||
} | ||
|
||
func (p *AzureDevOpsProvider) InitService(serviceName string, verbose bool) error { | ||
var isSupported bool | ||
if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported { | ||
return errors.New("azuredevpos: " + 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{}{ | ||
"organizationURL": p.organizationURL, | ||
"personalAccessToken": p.personalAccessToken, | ||
}) | ||
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,82 @@ | ||
// 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 azuredevpos | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops" | ||
"github.com/microsoft/azure-devops-go-api/azuredevops/core" | ||
"github.com/microsoft/azure-devops-go-api/azuredevops/git" | ||
"github.com/microsoft/azure-devops-go-api/azuredevops/graph" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
) | ||
|
||
type AzureDevOpsServiceGenerator interface { | ||
terraformutils.ServiceGenerator | ||
GetResourceConnections() map[string][]string | ||
} | ||
|
||
type AzureDevOpsService struct { //nolint | ||
terraformutils.Service | ||
} | ||
|
||
func (az *AzureDevOpsService) GetResourceConnections() map[string][]string { | ||
return nil | ||
} | ||
|
||
func (az *AzureDevOpsService) getConnection() *azuredevops.Connection { | ||
|
||
organizationURL := az.Args["organizationURL"].(string) | ||
personalAccessToken := az.Args["personalAccessToken"].(string) | ||
return azuredevops.NewPatConnection(organizationURL, personalAccessToken) | ||
} | ||
|
||
func (az *AzureDevOpsService) getCoreClient() (core.Client, error) { | ||
ctx := context.Background() | ||
client, err := core.NewClient(ctx, az.getConnection()) | ||
if err != nil { | ||
log.Println(err) | ||
return nil, err | ||
} | ||
return client, nil | ||
} | ||
|
||
func (az *AzureDevOpsService) getGraphClient() (graph.Client, error) { | ||
ctx := context.Background() | ||
client, err := graph.NewClient(ctx, az.getConnection()) | ||
if err != nil { | ||
log.Println(err) | ||
return nil, err | ||
} | ||
return client, nil | ||
} | ||
|
||
func (az *AzureDevOpsService) getGitClient() (git.Client, error) { | ||
ctx := context.Background() | ||
client, err := git.NewClient(ctx, az.getConnection()) | ||
if err != nil { | ||
log.Println(err) | ||
return nil, err | ||
} | ||
return client, nil | ||
} | ||
|
||
func (az *AzureDevOpsService) appendSimpleResource(id string, resourceName string, resourceType string) { | ||
newResource := terraformutils.NewSimpleResource(id, resourceName, resourceType, az.ProviderName, []string{}) | ||
az.Resources = append(az.Resources, newResource) | ||
} |
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,50 @@ | ||
package azuredevpos | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/git" | ||
) | ||
|
||
type GitRepositoryGenerator struct { | ||
AzureDevOpsService | ||
} | ||
|
||
func (az *GitRepositoryGenerator) listResources() ([]git.GitRepository, error) { | ||
|
||
client, err := az.getGitClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx := context.Background() | ||
resources, err := client.GetRepositories(ctx, git.GetRepositoriesArgs{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return *resources, nil | ||
} | ||
|
||
func (az *GitRepositoryGenerator) appendResource(resource *git.GitRepository) { | ||
|
||
id := *resource.Id | ||
az.appendSimpleResource(id.String(), *resource.Name, "azuredevops_git_repository") | ||
} | ||
|
||
func (az *GitRepositoryGenerator) InitResources() error { | ||
|
||
resources, err := az.listResources() | ||
if err != nil { | ||
return err | ||
} | ||
for _, resource := range resources { | ||
az.appendResource(&resource) | ||
} | ||
return nil | ||
} | ||
|
||
func (az *GitRepositoryGenerator) GetResourceConnections() map[string][]string { | ||
|
||
return map[string][]string{ | ||
"project": {"project_id", "id"}, | ||
} | ||
} |
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,58 @@ | ||
package azuredevpos | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/graph" | ||
) | ||
|
||
type GroupGenerator struct { | ||
AzureDevOpsService | ||
} | ||
|
||
func (az *GroupGenerator) listResources() ([]graph.GraphGroup, error) { | ||
|
||
client, fail := az.getGraphClient() | ||
if fail != nil { | ||
return nil, fail | ||
} | ||
ctx := context.Background() | ||
var resources []graph.GraphGroup | ||
pageArgs := graph.ListGroupsArgs{} | ||
pages, err := client.ListGroups(ctx, pageArgs) | ||
for ; err == nil; pages, err = client.ListGroups(ctx, pageArgs) { | ||
resources = append(resources, *pages.GraphGroups...) | ||
if pages.ContinuationToken == nil { | ||
return resources, nil | ||
} | ||
pageArgs = graph.ListGroupsArgs{ | ||
ContinuationToken: &(*pages.ContinuationToken)[0], | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
func (az *GroupGenerator) appendResource(resource *graph.GraphGroup) { | ||
|
||
resourceName := firstNonEmpty(resource.DisplayName, resource.MailAddress, resource.OriginId) | ||
az.appendSimpleResource(*resource.Descriptor, *resourceName, "azuredevops_group") | ||
} | ||
|
||
func (az *GroupGenerator) InitResources() error { | ||
|
||
resources, err := az.listResources() | ||
if err != nil { | ||
return err | ||
} | ||
for _, resource := range resources { | ||
az.appendResource(&resource) | ||
} | ||
return nil | ||
} | ||
|
||
func (az *GroupGenerator) GetResourceConnections() map[string][]string { | ||
|
||
return map[string][]string{ | ||
"project": {"scope", "id"}, | ||
} | ||
} |
Oops, something went wrong.