summaryrefslogtreecommitdiff
path: root/azuredevops
diff options
context:
space:
mode:
authorxuzhang3 <57888764+xuzhang3@users.noreply.github.com>2022-08-25 11:26:34 +0800
committerGitHub <noreply@github.com>2022-08-25 11:26:34 +0800
commit6ee1db81ea3e04fe32bf37b5659979bbfeee8a4e (patch)
treefb17572e06bc7af023dec798d34b99785cd66596 /azuredevops
parent22029987197e32fd4330a14efff65e76a748d84f (diff)
parent488f34861ff8d6baaa682221eee972351f191f58 (diff)
Merge pull request #627 from a30004053/feature/data_serviceendpoint_github
Create datasource azuredevops_serviceendpoint_github, add common func…
Diffstat (limited to 'azuredevops')
-rw-r--r--azuredevops/internal/acceptancetests/data_serviceendpoint_azurerm_test.go4
-rw-r--r--azuredevops/internal/acceptancetests/data_serviceendpoint_github_test.go61
-rw-r--r--azuredevops/internal/acceptancetests/testutils/hcl.go26
-rw-r--r--azuredevops/internal/service/serviceendpoint/commons.go127
-rw-r--r--azuredevops/internal/service/serviceendpoint/data_serviceendpoint_azurerm.go158
-rw-r--r--azuredevops/internal/service/serviceendpoint/data_serviceendpoint_github.go25
-rw-r--r--azuredevops/provider.go1
-rw-r--r--azuredevops/provider_test.go1
8 files changed, 252 insertions, 151 deletions
diff --git a/azuredevops/internal/acceptancetests/data_serviceendpoint_azurerm_test.go b/azuredevops/internal/acceptancetests/data_serviceendpoint_azurerm_test.go
index 2ede34db..3fdb4f96 100644
--- a/azuredevops/internal/acceptancetests/data_serviceendpoint_azurerm_test.go
+++ b/azuredevops/internal/acceptancetests/data_serviceendpoint_azurerm_test.go
@@ -32,7 +32,7 @@ func TestAccServiceEndpointAzureRM_with_serviceEndpointID_DataSource(t *testing.
Config: createServiceEndpointAzureRMWithServiceEndpointIDData,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", serviceEndpointAzureRMName),
- resource.TestCheckResourceAttrSet(tfNode, "id"),
+ resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_id"),
),
},
},
@@ -58,7 +58,7 @@ func TestAccServiceEndpointAzureRM_with_serviceEndpointName_DataSource(t *testin
Config: createServiceEndpointAzureRMWithServiceEndpointNameData,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", serviceEndpointAzureRMName),
- resource.TestCheckResourceAttrSet(tfNode, "id"),
+ resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_id"),
),
},
},
diff --git a/azuredevops/internal/acceptancetests/data_serviceendpoint_github_test.go b/azuredevops/internal/acceptancetests/data_serviceendpoint_github_test.go
new file mode 100644
index 00000000..aa468508
--- /dev/null
+++ b/azuredevops/internal/acceptancetests/data_serviceendpoint_github_test.go
@@ -0,0 +1,61 @@
+//go:build (all || data_sources || data_serviceendpoint_github) && (!exclude_data_sources || !exclude_data_serviceendpoint_github)
+// +build all data_sources data_serviceendpoint_github
+// +build !exclude_data_sources !exclude_data_serviceendpoint_github
+
+package acceptancetests
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
+ "github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/acceptancetests/testutils"
+)
+
+func TestAccServiceEndpointGitHub_with_serviceEndpointID_DataSource(t *testing.T) {
+ serviceEndpointGitHubName := testutils.GenerateResourceName()
+ projectName := testutils.GenerateResourceName()
+ createServiceEndpointGitHubWithServiceEndpointIDData := fmt.Sprintf("%s\n%s",
+ testutils.HclServiceEndpointGitHubResource(projectName, serviceEndpointGitHubName),
+ testutils.HclServiceEndpointGitHubDataSourceWithServiceEndpointID(),
+ )
+
+ tfNode := "data.azuredevops_serviceendpoint_github.serviceendpoint"
+ resource.ParallelTest(t, resource.TestCase{
+ PreCheck: func() { testutils.PreCheck(t, nil) },
+ Providers: testutils.GetProviders(),
+ Steps: []resource.TestStep{
+ {
+ Config: createServiceEndpointGitHubWithServiceEndpointIDData,
+ Check: resource.ComposeTestCheckFunc(
+ resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", serviceEndpointGitHubName),
+ resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_id"),
+ ),
+ },
+ },
+ })
+}
+
+func TestAccServiceEndpointGitHub_with_serviceEndpointName_DataSource(t *testing.T) {
+ serviceEndpointGitHubName := testutils.GenerateResourceName()
+ projectName := testutils.GenerateResourceName()
+ createServiceEndpointGitHubWithServiceEndpointNameData := fmt.Sprintf("%s\n%s",
+ testutils.HclServiceEndpointGitHubResource(projectName, serviceEndpointGitHubName),
+ testutils.HclServiceEndpointGitHubDataSourceWithServiceEndpointName(serviceEndpointGitHubName),
+ )
+
+ tfNode := "data.azuredevops_serviceendpoint_github.serviceendpoint"
+ resource.ParallelTest(t, resource.TestCase{
+ PreCheck: func() { testutils.PreCheck(t, nil) },
+ Providers: testutils.GetProviders(),
+ Steps: []resource.TestStep{
+ {
+ Config: createServiceEndpointGitHubWithServiceEndpointNameData,
+ Check: resource.ComposeTestCheckFunc(
+ resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", serviceEndpointGitHubName),
+ resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_id"),
+ ),
+ },
+ },
+ })
+}
diff --git a/azuredevops/internal/acceptancetests/testutils/hcl.go b/azuredevops/internal/acceptancetests/testutils/hcl.go
index 89e5f5ad..0b298fe7 100644
--- a/azuredevops/internal/acceptancetests/testutils/hcl.go
+++ b/azuredevops/internal/acceptancetests/testutils/hcl.go
@@ -251,6 +251,30 @@ resource "azuredevops_serviceendpoint_github" "serviceendpoint" {
return fmt.Sprintf("%s\n%s", projectResource, serviceEndpointResource)
}
+// HclServiceEndpointGitHubDataSourceWithServiceEndpointID HCL describing a data source for an AzDO service endpoint
+func HclServiceEndpointGitHubDataSourceWithServiceEndpointID() string {
+ serviceEndpointDataSource := fmt.Sprintf(`
+data "azuredevops_serviceendpoint_github" "serviceendpoint" {
+ project_id = azuredevops_project.project.id
+ service_endpoint_id = azuredevops_serviceendpoint_github.serviceendpoint.id
+}
+`)
+ return fmt.Sprintf("%s", serviceEndpointDataSource)
+}
+
+// HclServiceEndpointGitHubDataSourceWithServiceEndpointName HCL describing a data source for an AzDO service endpoint
+func HclServiceEndpointGitHubDataSourceWithServiceEndpointName(serviceEndpointName string) string {
+ serviceEndpointDataSource := fmt.Sprintf(`
+data "azuredevops_serviceendpoint_github" "serviceendpoint" {
+ project_id = azuredevops_project.project.id
+ service_endpoint_name = "%s"
+ depends_on = [azuredevops_serviceendpoint_github.serviceendpoint]
+}
+`, serviceEndpointName)
+
+ return fmt.Sprintf("%s", serviceEndpointDataSource)
+}
+
func HclServiceEndpointGitHubEnterpriseResource(projectName string, serviceEndpointName string) string {
serviceEndpointResource := fmt.Sprintf(`
resource "azuredevops_serviceendpoint_github_enterprise" "serviceendpoint" {
@@ -404,7 +428,7 @@ func HclServiceEndpointAzureRMDataSourceWithServiceEndpointID() string {
serviceEndpointDataSource := fmt.Sprintf(`
data "azuredevops_serviceendpoint_azurerm" "serviceendpointrm" {
project_id = azuredevops_project.project.id
- id = azuredevops_serviceendpoint_azurerm.serviceendpointrm.id
+ service_endpoint_id = azuredevops_serviceendpoint_azurerm.serviceendpointrm.id
}
`)
return fmt.Sprintf("%s", serviceEndpointDataSource)
diff --git a/azuredevops/internal/service/serviceendpoint/commons.go b/azuredevops/internal/service/serviceendpoint/commons.go
index 938f7341..5069f804 100644
--- a/azuredevops/internal/service/serviceendpoint/commons.go
+++ b/azuredevops/internal/service/serviceendpoint/commons.go
@@ -381,3 +381,130 @@ func getServiceEndpoint(client *client.AggregatedClient, serviceEndpointID *uuid
return nil, opState.Failed, fmt.Errorf(errMsgServiceCreate, serviceEndpointID, *projectID, serviceEndpoint.OperationStatus)
}
}
+
+func dataSourceGenBaseServiceEndpointResource(dataSourceReadFunc schema.ReadFunc) *schema.Resource {
+ return &schema.Resource{
+ Read: dataSourceReadFunc,
+ Schema: map[string]*schema.Schema{
+ "project_id": {
+ Type: schema.TypeString,
+ Required: true,
+ },
+
+ "service_endpoint_name": {
+ Type: schema.TypeString,
+ Optional: true,
+ Computed: true,
+ ExactlyOneOf: []string{"service_endpoint_name", "service_endpoint_id"},
+ ValidateFunc: validation.StringIsNotEmpty,
+ },
+
+ "service_endpoint_id": {
+ Description: "The ID of the serviceendpoint",
+ Type: schema.TypeString,
+ Optional: true,
+ Computed: true,
+ ExactlyOneOf: []string{"service_endpoint_name", "service_endpoint_id"},
+ ValidateFunc: validation.IsUUID,
+ },
+
+ "authorization": {
+ Type: schema.TypeMap,
+ Computed: true,
+ Elem: &schema.Schema{
+ Type: schema.TypeString,
+ },
+ },
+
+ "description": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+ },
+ }
+}
+
+func dataSourceMakeUnprotectedComputedSchema(r *schema.Resource, keyName string) {
+ r.Schema[keyName] = &schema.Schema{
+ Type: schema.TypeString,
+ Computed: true,
+ }
+}
+
+func dataSourceGetBaseServiceEndpoint(d *schema.ResourceData, m interface{}) (*serviceendpoint.ServiceEndpoint, *uuid.UUID, error) {
+ clients := m.(*client.AggregatedClient)
+
+ var projectID *uuid.UUID
+ projectIDString := d.Get("project_id").(string)
+ parsedProjectID, err := uuid.Parse(projectIDString)
+ if err != nil {
+ return nil, nil, fmt.Errorf("Error parsing projectID from the Terraform data source declaration: %v", err)
+ }
+
+ projectID = &parsedProjectID
+
+ if serviceEndpointIDString, ok := d.GetOk("service_endpoint_id"); ok {
+ var serviceEndpointID *uuid.UUID
+ parsedServiceEndpointID, err := uuid.Parse(serviceEndpointIDString.(string))
+ if err != nil {
+ return nil, nil, fmt.Errorf("Error parsing serviceEndpointID from the Terraform data source declaration: %v", err)
+ }
+ serviceEndpointID = &parsedServiceEndpointID
+
+ serviceEndpoint, err := clients.ServiceEndpointClient.GetServiceEndpointDetails(
+ clients.Ctx,
+ serviceendpoint.GetServiceEndpointDetailsArgs{
+ EndpointId: serviceEndpointID,
+ Project: converter.String(projectID.String()),
+ },
+ )
+ if err != nil {
+ if utils.ResponseWasNotFound(err) {
+ d.SetId("")
+ return nil, projectID, nil
+ }
+ return nil, projectID, fmt.Errorf("Error looking up service endpoint with ID (%v) and projectID (%v): %v", serviceEndpointID, projectID, err)
+ }
+
+ return serviceEndpoint, projectID, nil
+ }
+
+ if serviceEndpointName, ok := d.GetOk("service_endpoint_name"); ok {
+ serviceEndpoint, err := dataSourceGetServiceEndpointByNameAndProject(clients, serviceEndpointName.(string), projectID)
+ if err != nil {
+ if utils.ResponseWasNotFound(err) {
+ d.SetId("")
+ return nil, projectID, nil
+ }
+ return nil, projectID, fmt.Errorf("Error looking up service endpoint with name (%v) and projectID (%v): %v", serviceEndpointName, projectID, err)
+ }
+
+ return serviceEndpoint, projectID, nil
+ }
+ return nil, projectID, nil
+}
+
+func dataSourceGetServiceEndpointByNameAndProject(clients *client.AggregatedClient, serviceEndpointName string, projectID *uuid.UUID) (*serviceendpoint.ServiceEndpoint, error) {
+ serviceEndpointNameList := &[]string{serviceEndpointName}
+
+ serviceEndpoints, err := clients.ServiceEndpointClient.GetServiceEndpointsByNames(
+ clients.Ctx,
+ serviceendpoint.GetServiceEndpointsByNamesArgs{
+ Project: converter.String(projectID.String()),
+ EndpointNames: serviceEndpointNameList,
+ },
+ )
+ if err != nil {
+ return nil, err
+ }
+
+ if len(*serviceEndpoints) == 0 {
+ return nil, fmt.Errorf("%v not found!", serviceEndpointName)
+ }
+
+ if len(*serviceEndpoints) > 1 {
+ return nil, fmt.Errorf("%v returns more than one serviceEndpoint!", serviceEndpointName)
+ }
+
+ return &(*serviceEndpoints)[0], nil
+}
diff --git a/azuredevops/internal/service/serviceendpoint/data_serviceendpoint_azurerm.go b/azuredevops/internal/service/serviceendpoint/data_serviceendpoint_azurerm.go
index fdbd63e9..a50b1f54 100644
--- a/azuredevops/internal/service/serviceendpoint/data_serviceendpoint_azurerm.go
+++ b/azuredevops/internal/service/serviceendpoint/data_serviceendpoint_azurerm.go
@@ -3,166 +3,28 @@ package serviceendpoint
import (
"fmt"
- "github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
- "github.com/microsoft/azure-devops-go-api/azuredevops/v6/serviceendpoint"
- "github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/client"
- "github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils"
- "github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils/converter"
)
func DataServiceEndpointAzureRM() *schema.Resource {
- return &schema.Resource{
- Read: dataSourceServiceEndpointAzureRMRead,
- Schema: map[string]*schema.Schema{
- "project_id": {
- Type: schema.TypeString,
- Required: true,
- },
-
- "service_endpoint_name": {
- Type: schema.TypeString,
- Optional: true,
- Computed: true,
- ExactlyOneOf: []string{"service_endpoint_name", "id"},
- ValidateFunc: validation.StringIsNotEmpty,
- },
-
- "id": {
- Description: "The ID of the serviceendpoint",
- Type: schema.TypeString,
- Optional: true,
- Computed: true,
- ExactlyOneOf: []string{"service_endpoint_name", "id"},
- ValidateFunc: validation.IsUUID,
- },
-
- "authorization": {
- Type: schema.TypeMap,
- Computed: true,
- Elem: &schema.Schema{
- Type: schema.TypeString,
- },
- },
-
- "azurerm_management_group_id": {
- Type: schema.TypeString,
- Computed: true,
- },
-
- "azurerm_management_group_name": {
- Type: schema.TypeString,
- Computed: true,
- },
-
- "azurerm_subscription_id": {
- Type: schema.TypeString,
- Computed: true,
- },
-
- "azurerm_subscription_name": {
- Type: schema.TypeString,
- Computed: true,
- },
-
- "resource_group": {
- Type: schema.TypeString,
- Computed: true,
- },
-
- "azurerm_spn_tenantid": {
- Type: schema.TypeString,
- Computed: true,
- },
-
- "description": {
- Type: schema.TypeString,
- Computed: true,
- },
- },
+ r := dataSourceGenBaseServiceEndpointResource(dataSourceServiceEndpointAzureRMRead)
+ schemaKeys := []string{"azurerm_management_group_id", "azurerm_management_group_name", "azurerm_subscription_id", "azurerm_subscription_name", "resource_group", "azurerm_spn_tenantid"}
+ for _, k := range schemaKeys {
+ dataSourceMakeUnprotectedComputedSchema(r, k)
}
+ return r
}
func dataSourceServiceEndpointAzureRMRead(d *schema.ResourceData, m interface{}) error {
- clients := m.(*client.AggregatedClient)
-
- var projectID *uuid.UUID
- projectIDString := d.Get("project_id").(string)
- parsedProjectID, err := uuid.Parse(projectIDString)
+ serviceEndpoint, projectID, err := dataSourceGetBaseServiceEndpoint(d, m)
if err != nil {
- return fmt.Errorf("Error parsing projectID from the Terraform data source declaration: %v", err)
+ return err
}
-
- projectID = &parsedProjectID
-
- if serviceEndpointIDString, ok := d.GetOk("id"); ok {
- var serviceEndpointID *uuid.UUID
- parsedServiceEndpointID, err := uuid.Parse(serviceEndpointIDString.(string))
- if err != nil {
- return fmt.Errorf("Error parsing serviceEndpointID from the Terraform data source declaration: %v", err)
- }
- serviceEndpointID = &parsedServiceEndpointID
-
- serviceEndpoint, err := clients.ServiceEndpointClient.GetServiceEndpointDetails(
- clients.Ctx,
- serviceendpoint.GetServiceEndpointDetailsArgs{
- EndpointId: serviceEndpointID,
- Project: converter.String(projectID.String()),
- },
- )
- if err != nil {
- if utils.ResponseWasNotFound(err) {
- d.SetId("")
- return nil
- }
- return fmt.Errorf("Error looking up service endpoint with ID (%v) and projectID (%v): %v", serviceEndpointID, projectID, err)
- }
-
+ if serviceEndpoint != nil {
(*serviceEndpoint.Data)["creationMode"] = ""
+ d.Set("service_endpoint_id", serviceEndpoint.Id.String())
flattenServiceEndpointAzureRM(d, serviceEndpoint, projectID)
return nil
}
-
- if serviceEndpointName, ok := d.GetOk("service_endpoint_name"); ok {
- // get service endpointdetails by name
- serviceEndpoint, err := getServiceEndpointByNameAndProject(clients, serviceEndpointName.(string), projectID)
- if err != nil {
- if utils.ResponseWasNotFound(err) {
- d.SetId("")
- return nil
- }
- return fmt.Errorf("Error looking up service endpoint with name (%v) and projectID (%v): %v", serviceEndpointName, projectID, err)
- }
-
- (*serviceEndpoint.Data)["creationMode"] = ""
- flattenServiceEndpointAzureRM(d, serviceEndpoint, projectID)
- return nil
- }
- return nil
-}
-
-func getServiceEndpointByNameAndProject(clients *client.AggregatedClient, serviceEndpointName string, projectID *uuid.UUID) (*serviceendpoint.ServiceEndpoint, error) {
- serviceEndpointNameList := &[]string{serviceEndpointName}
-
- serviceEndpoints, err := clients.ServiceEndpointClient.GetServiceEndpointsByNames(
- clients.Ctx,
- serviceendpoint.GetServiceEndpointsByNamesArgs{
- Project: converter.String(projectID.String()),
- EndpointNames: serviceEndpointNameList,
- },
- )
- if err != nil {
- return nil, err
- }
-
- if len(*serviceEndpoints) == 0 {
- return nil, fmt.Errorf("%v not found!", serviceEndpointName)
- }
-
- if len(*serviceEndpoints) > 1 {
- return nil, fmt.Errorf("%v returns more than one serviceEndpoint!", serviceEndpointName)
- }
-
- return &(*serviceEndpoints)[0], nil
+ return fmt.Errorf("Error looking up service endpoint!")
}
diff --git a/azuredevops/internal/service/serviceendpoint/data_serviceendpoint_github.go b/azuredevops/internal/service/serviceendpoint/data_serviceendpoint_github.go
new file mode 100644
index 00000000..7cccc100
--- /dev/null
+++ b/azuredevops/internal/service/serviceendpoint/data_serviceendpoint_github.go
@@ -0,0 +1,25 @@
+package serviceendpoint
+
+import (
+ "fmt"
+
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+)
+
+func DataServiceEndpointGithub() *schema.Resource {
+ r := dataSourceGenBaseServiceEndpointResource(dataSourceServiceEndpointGithubRead)
+ return r
+}
+
+func dataSourceServiceEndpointGithubRead(d *schema.ResourceData, m interface{}) error {
+ serviceEndpoint, projectID, err := dataSourceGetBaseServiceEndpoint(d, m)
+ if err != nil {
+ return err
+ }
+ if serviceEndpoint != nil {
+ d.Set("service_endpoint_id", serviceEndpoint.Id.String())
+ doBaseFlattening(d, serviceEndpoint, projectID)
+ return nil
+ }
+ return fmt.Errorf("Error looking up service endpoint!")
+}
diff --git a/azuredevops/provider.go b/azuredevops/provider.go
index 02776696..a179ac00 100644
--- a/azuredevops/provider.go
+++ b/azuredevops/provider.go
@@ -106,6 +106,7 @@ func Provider() *schema.Provider {
"azuredevops_groups": graph.DataGroups(),
"azuredevops_variable_group": taskagent.DataVariableGroup(),
"azuredevops_serviceendpoint_azurerm": serviceendpoint.DataServiceEndpointAzureRM(),
+ "azuredevops_serviceendpoint_github": serviceendpoint.DataServiceEndpointGithub(),
},
Schema: map[string]*schema.Schema{
"org_service_url": {
diff --git a/azuredevops/provider_test.go b/azuredevops/provider_test.go
index 6b642542..518598db 100644
--- a/azuredevops/provider_test.go
+++ b/azuredevops/provider_test.go
@@ -102,6 +102,7 @@ func TestProvider_HasChildDataSources(t *testing.T) {
"azuredevops_groups",
"azuredevops_variable_group",
"azuredevops_serviceendpoint_azurerm",
+ "azuredevops_serviceendpoint_github",
}
dataSources := Provider().DataSourcesMap