generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [CDS-105076]: Add support for azure artifacts connector (#1141)
* feat: [CDS-105076]: Add support for azure artifacts connector * feat: [CDS-105076]: Dependency update
- Loading branch information
1 parent
4949d29
commit bd90a55
Showing
6 changed files
with
282 additions
and
5 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 |
---|---|---|
|
@@ -41,3 +41,4 @@ test-report.xml | |
terraform-provider-harness | ||
|
||
.terraform.lock.hcl | ||
local.sh |
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
138 changes: 138 additions & 0 deletions
138
internal/service/cd_nextgen/connector/artifactRepositories/azure_artifacts.go
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,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 | ||
} |
136 changes: 136 additions & 0 deletions
136
internal/service/cd_nextgen/connector/artifactRepositories/azure_artifacts_test.go
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,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) | ||
} |