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

fix: [CDS-75383]: support for execute on delegate option for docker connector #641

Merged
merged 8 commits into from
Aug 11, 2023
3 changes: 3 additions & 0 deletions .changelog/651.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:fix
resource/harness_platform_connector_docker: adding support for execute_on_delegate.
rathodmeetsatish marked this conversation as resolved.
Show resolved Hide resolved
```
1 change: 1 addition & 0 deletions docs/resources/platform_connector_docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ resource "harness_platform_connector_docker" "test" {
- `org_id` (String) Unique identifier of the organization.
- `project_id` (String) Unique identifier of the project.
- `tags` (Set of String) Tags to associate with the resource.
- `execute_on_delegate` (Boolean) Execute on delegate or not.

### Read-Only

Expand Down
11 changes: 11 additions & 0 deletions internal/service/platform/connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func ResourceConnectorDocker() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"execute_on_delegate": {
Description: "Execute on delegate or not.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"credentials": {
Description: "The credentials to use for the docker registry. If not specified then the connection is made to the registry anonymously.",
Type: schema.TypeList,
Expand Down Expand Up @@ -151,6 +157,10 @@ func buildConnectorDocker(d *schema.ResourceData) *nextgen.ConnectorInfo {
if attr, ok := config["password_ref"]; ok {
connector.DockerRegistry.Auth.UsernamePassword.PasswordRef = attr.(string)
}

}
if attr, ok := d.GetOk("execute_on_delegate"); ok {
connector.DockerRegistry.ExecuteOnDelegate = attr.(bool)
}

return connector
Expand All @@ -160,6 +170,7 @@ func readConnectorDocker(d *schema.ResourceData, connector *nextgen.ConnectorInf
d.Set("type", connector.DockerRegistry.ProviderType)
d.Set("url", connector.DockerRegistry.DockerRegistryUrl)
d.Set("delegate_selectors", connector.DockerRegistry.DelegateSelectors)
d.Set("execute_on_delegate", connector.DockerRegistry.ExecuteOnDelegate)

switch connector.DockerRegistry.Auth.Type_ {
case nextgen.DockerAuthTypes.UsernamePassword:
Expand Down
5 changes: 5 additions & 0 deletions internal/service/platform/connector/docker_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func DatasourceConnectorDocker() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"execute_on_delegate": {
Description: "Execute on delegate or not.",
Type: schema.TypeBool,
Computed: true,
},
},
},
},
Expand Down
184 changes: 184 additions & 0 deletions internal/service/platform/connector/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestAccResourceConnectorDocker_DockerHub(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "url", "https://hub.docker.com"),
resource.TestCheckResourceAttr(resourceName, "delegate_selectors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "credentials.0.username", "admin"),
resource.TestCheckResourceAttr(resourceName, "execute_on_delegate", "true"),
),
},
{
Expand All @@ -50,6 +51,7 @@ func TestAccResourceConnectorDocker_DockerHub(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "url", "https://hub.docker.com"),
resource.TestCheckResourceAttr(resourceName, "delegate_selectors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "credentials.0.username", "admin"),
resource.TestCheckResourceAttr(resourceName, "execute_on_delegate", "true"),
),
},
{
Expand All @@ -61,6 +63,116 @@ func TestAccResourceConnectorDocker_DockerHub(t *testing.T) {
})
}

func TestAccResourceConnectorDocker_DockerHubExecuteOnDelegateFalse(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
name := id
updatedName := fmt.Sprintf("%s_updated", name)
resourceName := "harness_platform_connector_docker.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: testAccResourceConnectorDocker_DockerHubExecuteOnDelegateFalse(id, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "type", "DockerHub"),
resource.TestCheckResourceAttr(resourceName, "url", "https://hub.docker.com"),
resource.TestCheckResourceAttr(resourceName, "delegate_selectors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "credentials.0.username", "admin"),
resource.TestCheckResourceAttr(resourceName, "execute_on_delegate", "false"),
),
},
{
Config: testAccResourceConnectorDocker_DockerHubExecuteOnDelegateFalse(id, updatedName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", updatedName),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "type", "DockerHub"),
resource.TestCheckResourceAttr(resourceName, "url", "https://hub.docker.com"),
resource.TestCheckResourceAttr(resourceName, "delegate_selectors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "credentials.0.username", "admin"),
resource.TestCheckResourceAttr(resourceName, "execute_on_delegate", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"execute_on_delegate"},
},
},
})
}

func TestAccResourceConnectorDocker_DockerHubExecuteOnDelegateTrue(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
name := id
updatedName := fmt.Sprintf("%s_updated", name)
resourceName := "harness_platform_connector_docker.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: testAccResourceConnectorDocker_DockerHubExecuteOnDelegateTrue(id, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "type", "DockerHub"),
resource.TestCheckResourceAttr(resourceName, "url", "https://hub.docker.com"),
resource.TestCheckResourceAttr(resourceName, "delegate_selectors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "credentials.0.username", "admin"),
resource.TestCheckResourceAttr(resourceName, "execute_on_delegate", "true"),
),
},
{
Config: testAccResourceConnectorDocker_DockerHubExecuteOnDelegateTrue(id, updatedName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", updatedName),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "type", "DockerHub"),
resource.TestCheckResourceAttr(resourceName, "url", "https://hub.docker.com"),
resource.TestCheckResourceAttr(resourceName, "delegate_selectors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "credentials.0.username", "admin"),
resource.TestCheckResourceAttr(resourceName, "execute_on_delegate", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"execute_on_delegate"},
},
},
})
}

func TestAccResourceConnectorDocker_DockerHub_Anonymous(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
Expand Down Expand Up @@ -130,6 +242,78 @@ func testAccResourceConnectorDocker_DockerHub(id string, name string) string {
`, id, name)
}

func testAccResourceConnectorDocker_DockerHubExecuteOnDelegateFalse(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_secret_text" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
tags = ["foo:bar"]

secret_manager_identifier = "harnessSecretManager"
value_type = "Inline"
value = "secret"
}

resource "harness_platform_connector_docker" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
tags = ["foo:bar"]
execute_on_delegate = false
type = "DockerHub"
url = "https://hub.docker.com"
delegate_selectors = ["harness-delegate"]
credentials {
username = "admin"
password_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 testAccResourceConnectorDocker_DockerHubExecuteOnDelegateTrue(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_secret_text" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
tags = ["foo:bar"]

secret_manager_identifier = "harnessSecretManager"
value_type = "Inline"
value = "secret"
}

resource "harness_platform_connector_docker" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
tags = ["foo:bar"]
execute_on_delegate = true
type = "DockerHub"
url = "https://hub.docker.com"
delegate_selectors = ["harness-delegate"]
credentials {
username = "admin"
password_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 testAccResourceConnectorDocker_anonymous(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_connector_docker" "test" {
Expand Down