Skip to content

Commit

Permalink
feat: [CDS-105076]: Add support for azure artifacts connector (#1141)
Browse files Browse the repository at this point in the history
* feat: [CDS-105076]: Add support for azure artifacts connector

* feat: [CDS-105076]: Dependency update
  • Loading branch information
Fernando-Dourado authored Jan 7, 2025
1 parent 4949d29 commit bd90a55
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ test-report.xml
terraform-provider-harness

.terraform.lock.hcl
local.sh
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/antihax/optional v1.0.0
github.com/aws/aws-sdk-go v1.46.4
github.com/docker/docker v24.0.5+incompatible
github.com/harness/harness-go-sdk v0.4.23
github.com/harness/harness-go-sdk v0.4.24
github.com/harness/harness-openapi-go-client v0.0.21
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/harness/harness-go-sdk v0.4.23 h1:iPh1v1sdZ1So1CdtqRkX+YY2hXTApaJsDt0XZWwa+hs=
github.com/harness/harness-go-sdk v0.4.23/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
github.com/harness/harness-go-sdk v0.4.24 h1:pfN0JdnrwzWgt1OB4SinOGTJp1yy4RCiJr1xpPYOGd0=
github.com/harness/harness-go-sdk v0.4.24/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
github.com/harness/harness-openapi-go-client v0.0.21 h1:VtJnpQKZvCAlaCmUPbNR69OT3c5WRdhNN5TOgUwtwZ4=
github.com/harness/harness-openapi-go-client v0.0.21/go.mod h1:u0vqYb994BJGotmEwJevF4L3BNAdU9i8ui2d22gmLPA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
6 changes: 4 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package provider
import (
"context"
"fmt"
"github.com/harness/terraform-provider-harness/internal/service/platform/module_registry"
"log"

cdng_service "github.com/harness/terraform-provider-harness/internal/service/cd_nextgen/service"
"github.com/harness/terraform-provider-harness/internal/service/platform/module_registry"
"github.com/harness/terraform-provider-harness/internal/service/platform/service_account"
"log"

pipeline_gitx "github.com/harness/terraform-provider-harness/internal/service/pipeline/gitx/webhook"
"github.com/harness/terraform-provider-harness/internal/service/platform/cluster_orchestrator"
Expand Down Expand Up @@ -306,6 +307,7 @@ func Provider(version string) func() *schema.Provider {
"harness_platform_connector_appdynamics": connector.ResourceConnectorAppDynamics(),
"harness_platform_connector_elasticsearch": connector.ResourceConnectorElasticSearch(),
"harness_platform_connector_artifactory": cdng_connector_artifactRepositories.ResourceConnectorArtifactory(),
"harness_platform_connector_azure_artifacts": cdng_connector_artifactRepositories.ResourceConnectorAzureArtifacts(),
"harness_platform_connector_aws_secret_manager": pl_secretManagers.ResourceConnectorAwsSM(),
"harness_platform_connector_aws": cdng_connector_cloudProviders.ResourceConnectorAws(),
"harness_platform_connector_awscc": connector.ResourceConnectorAwsCC(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package artifactRepositories

import (
"context"
"fmt"

"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceConnectorAzureArtifacts() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating an Azure Artifacts connector.",
ReadContext: resourceConnectorAzureArtifactsRead,
CreateContext: resourceConnectorAzureArtifactsCreateOrUpdate,
UpdateContext: resourceConnectorAzureArtifactsCreateOrUpdate,
DeleteContext: resourceConnectorDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"url": {
Description: "URL of the Azure Artifacts server.",
Type: schema.TypeString,
Required: true,
},
"delegate_selectors": {
Description: "Tags to filter delegates for connection.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"credentials": {
Description: "Credentials to use for authentication.",
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"token_ref": {
Description: "Reference to a secret containing the token to use for authentication." + secret_ref_text,
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}

helpers.SetMultiLevelResourceSchema(resource.Schema)

return resource
}

func resourceConnectorAzureArtifactsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := resourceConnectorReadBase(ctx, d, meta, nextgen.ConnectorTypes.AzureArtifacts)
if err != nil {
return err
}

if conn == nil {
return nil
}

if err := readConnectorAzureArtifacts(d, conn); err != nil {
return diag.FromErr(err)
}

return nil
}

func readConnectorAzureArtifacts(d *schema.ResourceData, connector *nextgen.ConnectorInfo) error {

d.Set("url", connector.AzureArtifacts.Url)
d.Set("delegate_selectors", connector.AzureArtifacts.DelegateSelectors)

switch connector.AzureArtifacts.Auth.Creds.Type_ {
case nextgen.AzureArtifactsAuthTypes.PersonalAccessToken:
d.Set("credentials", []map[string]interface{}{
{
"token_ref": connector.AzureArtifacts.Auth.Creds.UserToken.TokenRef,
},
})
default:
return fmt.Errorf("unsupported auth type: %s", connector.AzureArtifacts.Auth.Creds.Type_)
}

return nil
}

func resourceConnectorAzureArtifactsCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := buildConnectorAzureArtifacts(d)

newConn, err := resourceConnectorCreateOrUpdateBase(ctx, d, meta, conn)
if err != nil {
return err
}

if err := readConnectorAzureArtifacts(d, newConn); err != nil {
return diag.FromErr(err)
}

return nil
}

func buildConnectorAzureArtifacts(d *schema.ResourceData) *nextgen.ConnectorInfo {
connector := &nextgen.ConnectorInfo{
Type_: nextgen.ConnectorTypes.AzureArtifacts,
AzureArtifacts: &nextgen.AzureArtifactsConnector{
Auth: &nextgen.AzureArtifactsAuthentication{
Creds: &nextgen.AzureArtifactsHttpCredentials{},
},
},
}

if attr, ok := d.GetOk("url"); ok {
connector.AzureArtifacts.Url = attr.(string)
}

if attr, ok := d.GetOk("delegate_selectors"); ok {
connector.AzureArtifacts.DelegateSelectors = utils.InterfaceSliceToStringSlice(attr.(*schema.Set).List())
}

if attr, ok := d.GetOk("credentials"); ok {
config := attr.([]interface{})[0].(map[string]interface{})

connector.AzureArtifacts.Auth.Creds.Type_ = nextgen.AzureArtifactsAuthTypes.PersonalAccessToken
connector.AzureArtifacts.Auth.Creds.UserToken = &nextgen.AzureArtifactsUsernameToken{}

if attr, ok := config["token_ref"]; ok {
connector.AzureArtifacts.Auth.Creds.UserToken.TokenRef = attr.(string)
}
}

return connector
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package artifactRepositories_test

import (
"fmt"
"testing"

"github.com/harness/harness-go-sdk/harness/utils"
"github.com/harness/terraform-provider-harness/internal/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccResourceConnectorAzureArtifacts(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
name := id
resourceName := "harness_platform_connector_azure_artifacts.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"time": {},
},
CheckDestroy: testAccConnectorDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceConnectorAzureArtifacts(id, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "url", "https://az-artifacts.example.com"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccResourceConnectorAzureArtifactsWithDelegateSelectors(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
name := id
resourceName := "harness_platform_connector_azure_artifacts.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"time": {},
},
CheckDestroy: testAccConnectorDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceConnectorAzureArtifactsWithDelegateSelectors(id, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "url", "https://az-artifacts.example.com"),
resource.TestCheckResourceAttr(resourceName, "delegate_selectors.#", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccResourceConnectorAzureArtifacts(id, name string) string {
return fmt.Sprintf(`
resource "harness_platform_secret_text" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
secret_manager_identifier = "harnessSecretManager"
value_type = "Inline"
value = "secret"
}
resource "harness_platform_connector_azure_artifacts" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
url = "https://az-artifacts.example.com"
credentials {
token_ref = "account.${harness_platform_secret_text.test.id}"
}
depends_on = [time_sleep.wait_4_seconds]
}
resource "time_sleep" "wait_4_seconds" {
depends_on = [harness_platform_secret_text.test]
destroy_duration = "4s"
}
`, id, name)
}

func testAccResourceConnectorAzureArtifactsWithDelegateSelectors(id, name string) string {
return fmt.Sprintf(`
resource "harness_platform_secret_text" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
secret_manager_identifier = "harnessSecretManager"
value_type = "Inline"
value = "secret"
}
resource "harness_platform_connector_azure_artifacts" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
url = "https://az-artifacts.example.com"
credentials {
token_ref = "account.${harness_platform_secret_text.test.id}"
}
delegate_selectors = ["harness-delegate"]
depends_on = [time_sleep.wait_4_seconds]
}
resource "time_sleep" "wait_4_seconds" {
depends_on = [harness_platform_secret_text.test]
destroy_duration = "4s"
}
`, id, name)
}

0 comments on commit bd90a55

Please sign in to comment.