Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add JFrog V2 Service Endpoint Data Source (Issue #1231) #1232

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build (all || data_sources || data_serviceendpoint_jfrog_artifactory_v2) && (!exclude_data_sources || !exclude_data_serviceendpoint_jfrog_artifactory_v2)
// +build all data_sources data_serviceendpoint_jfrog_artifactory_v2
// +build !exclude_data_sources !exclude_data_serviceendpoint_jfrog_artifactory_v2

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 TestAccServiceEndpointJfrogArtifactoryV2_dataSource(t *testing.T) {
name := testutils.GenerateResourceName()

tfNode := "data.azuredevops_serviceendpoint_jfrog_artifactory_v2.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
Config: hclServiceEndpointJfrogArtifactoryV2DataSource(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", name),
resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_name"),
),
},
},
})
}

func hclServiceEndpointJfrogArtifactoryV2DataSource(name string) string {
return fmt.Sprintf(`
resource "azuredevops_project" "test" {
name = "%[1]s"
visibility = "private"
version_control = "Git"
work_item_template = "Agile"
}

resource "azuredevops_serviceendpoint_jfrog_artifactory_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = "%[1]s"
token = "0000000000000000000000000000000000000000"
description = "Managed by Terraform"
}

data "azuredevops_serviceendpoint_jfrog_artifactory_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = azuredevops_serviceendpoint_jfrog_artifactory_v2.test.service_endpoint_name
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build (all || data_sources || data_serviceendpoint_jfrog_distribution_v2) && (!exclude_data_sources || !exclude_data_serviceendpoint_jfrog_distribution_v2)
// +build all data_sources data_serviceendpoint_jfrog_distribution_v2
// +build !exclude_data_sources !exclude_data_serviceendpoint_jfrog_distribution_v2

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 TestAccServiceEndpointJfrogDistributionV2_dataSource(t *testing.T) {
name := testutils.GenerateResourceName()

tfNode := "data.azuredevops_serviceendpoint_jfrog_distribution_v2.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
Config: hclServiceEndpointJfrogDistributionV2DataSource(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", name),
resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_name"),
),
},
},
})
}

func hclServiceEndpointJfrogDistributionV2DataSource(name string) string {
return fmt.Sprintf(`
resource "azuredevops_project" "test" {
name = "%[1]s"
visibility = "private"
version_control = "Git"
work_item_template = "Agile"
}

resource "azuredevops_serviceendpoint_jfrog_distribution_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = "%[1]s"
token = "0000000000000000000000000000000000000000"
description = "Managed by Terraform"
}

data "azuredevops_serviceendpoint_jfrog_distribution_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = azuredevops_serviceendpoint_jfrog_distribution_v2.test.service_endpoint_name
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build (all || data_sources || data_serviceendpoint_jfrog_platform_v2) && (!exclude_data_sources || !exclude_data_serviceendpoint_jfrog_platform_v2)
// +build all data_sources data_serviceendpoint_jfrog_platform_v2
// +build !exclude_data_sources !exclude_data_serviceendpoint_jfrog_platform_v2

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 TestAccServiceEndpointJfrogPlatformV2_dataSource(t *testing.T) {
name := testutils.GenerateResourceName()

tfNode := "data.azuredevops_serviceendpoint_jfrog_platform_v2.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
Config: hclServiceEndpointJfrogPlatformV2DataSource(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", name),
resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_name"),
),
},
},
})
}

func hclServiceEndpointJfrogPlatformV2DataSource(name string) string {
return fmt.Sprintf(`
resource "azuredevops_project" "test" {
name = "%[1]s"
visibility = "private"
version_control = "Git"
work_item_template = "Agile"
}

resource "azuredevops_serviceendpoint_jfrog_platform_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = "%[1]s"
token = "0000000000000000000000000000000000000000"
description = "Managed by Terraform"
}

data "azuredevops_serviceendpoint_jfrog_platform_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = azuredevops_serviceendpoint_jfrog_platform_v2.test.service_endpoint_name
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build (all || data_sources || data_serviceendpoint_jfrog_xray_v2) && (!exclude_data_sources || !exclude_data_serviceendpoint_jfrog_xray_v2)
// +build all data_sources data_serviceendpoint_jfrog_xray_v2
// +build !exclude_data_sources !exclude_data_serviceendpoint_jfrog_xray_v2

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 TestAccServiceEndpointJfrogXrayV2_dataSource(t *testing.T) {
name := testutils.GenerateResourceName()

tfNode := "data.azuredevops_serviceendpoint_jfrog_xray_v2.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testutils.PreCheck(t, nil) },
Providers: testutils.GetProviders(),
Steps: []resource.TestStep{
{
Config: hclServiceEndpointJfrogXrayV2DataSource(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(tfNode, "service_endpoint_name", name),
resource.TestCheckResourceAttrSet(tfNode, "service_endpoint_name"),
),
},
},
})
}

func hclServiceEndpointJfrogXrayV2DataSource(name string) string {
return fmt.Sprintf(`
resource "azuredevops_project" "test" {
name = "%[1]s"
visibility = "private"
version_control = "Git"
work_item_template = "Agile"
}

resource "azuredevops_serviceendpoint_jfrog_xray_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = "%[1]s"
token = "0000000000000000000000000000000000000000"
description = "Managed by Terraform"
}

data "azuredevops_serviceendpoint_jfrog_xray_v2" "test" {
project_id = azuredevops_project.test.id
service_endpoint_name = azuredevops_serviceendpoint_jfrog_xray_v2.test.service_endpoint_name
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package serviceendpoint

import (
"fmt"
"maps"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"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/tfhelper"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils/validate"
)

// DataSourceServiceEndpointJFrogArtifactoryV2 schema and implementation for JFrog Artifactory service endpoint resource
func DataSourceServiceEndpointJFrogArtifactoryV2() *schema.Resource {
r := &schema.Resource{
Read: DataSourceServiceEndpointJFrogArtifactoryV2Read,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(1 * time.Minute),
},
Importer: tfhelper.ImportProjectQualifiedResourceUUID(),
jasonouellet marked this conversation as resolved.
Show resolved Hide resolved
Schema: baseSchema(),
jasonouellet marked this conversation as resolved.
Show resolved Hide resolved
}

maps.Copy(r.Schema, map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.Url,
Description: "Url for the JFrog Artifactory Server",
jasonouellet marked this conversation as resolved.
Show resolved Hide resolved
},

"authentication_token": {
jasonouellet marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"token": {
Description: "The JFrog Artifactory access token.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
},
},
ExactlyOneOf: []string{"authentication_basic", "authentication_token"},
},

"authentication_basic": {
jasonouellet marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Description: "The JFrog Artifactory user name.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"password": {
Description: "The JFrog Artifactory password.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
},
},
},
})

return r
}

func DataSourceServiceEndpointJFrogArtifactoryV2Read(d *schema.ResourceData, m interface{}) error {
clients := m.(*client.AggregatedClient)
getArgs, err := serviceEndpointGetArgs(d)
if err != nil {
return err
}

serviceEndpoint, err := clients.ServiceEndpointClient.GetServiceEndpointDetails(clients.Ctx, *getArgs)
if err != nil {
if utils.ResponseWasNotFound(err) {
d.SetId("")
return nil
}
return fmt.Errorf(" looking up service endpoint given ID (%v) and project ID (%v): %v", getArgs.EndpointId, getArgs.Project, err)
}

if err = checkServiceConnection(serviceEndpoint); err != nil {
return err
}
flattenServiceEndpointArtifactoryV2(d, serviceEndpoint)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flattenServiceEndpointArtifactoryV2(d, serviceEndpoint)
d.Set("url", serviceEndpoint.Url)

return nil
}
Loading