generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [PL-39175]: PR for adding delegate token resource (#719)
* fix: [PL-39175]: draft pr for adding delegate token resource * fix: [PL-39175]: minor fix * fix: [PL-39175]: Added tests * fix: [PL-39175]: Fixed tests * fix: [PL-39175]: Added documentation
- Loading branch information
1 parent
7dcb881
commit e72b2a9
Showing
8 changed files
with
755 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
resource/harness_platform_delegatetoken: Added delegate token resource. | ||
``` |
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,45 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "harness_platform_delegatetoken Data Source - terraform-provider-harness" | ||
subcategory: "Next Gen" | ||
description: |- | ||
Data Source for retrieving delegate tokens. | ||
--- | ||
|
||
# harness_platform_delegatetoken (Data Source) | ||
|
||
Data Source for retrieving delegate tokens. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "harness_platform_token" "test" { | ||
identifier = "test_token" | ||
name = "test token" | ||
account_id = "account_id" | ||
org_id = "org_id" | ||
project_id = "project_id" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `account_id` (String) Account Identifier for the Entity | ||
- `identifier` (String) Unique identifier of the resource. | ||
- `name` (String) Name of the resource. | ||
|
||
### Optional | ||
|
||
- `token_status` (String) Status of Delegate Token (ACTIVE or REVOKED). If left empty both active and revoked tokens will be assumed. | ||
- `created_at` (String) Time when the delegate token is created. | ||
- `created_by` (String) created by details. | ||
- `org_id` (String) Unique identifier of the organization. | ||
- `project_id` (String) Unique identifier of the project. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `value` (String, Sensitive) Value of the delegate Token |
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,76 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "harness_platform_delegatetoken Resource - terraform-provider-harness" | ||
subcategory: "Next Gen" | ||
description: |- | ||
Resource for creating delegate tokens. | ||
--- | ||
|
||
# harness_platform_delegatetoken (Resource) | ||
|
||
Resource for creating delegate tokens. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
# Create delegate token for account level | ||
resource "harness_platform_delegatetoken" "test" { | ||
identifier = "test_token" | ||
name = "test token" | ||
account_id = "account_id" | ||
} | ||
# Create token for org level apikey | ||
resource "harness_platform_token" "test" { | ||
identifier = "test_token" | ||
name = "test token" | ||
account_id = "account_id" | ||
org_id = "org_id" | ||
} | ||
# Create token for project level apikey | ||
resource "harness_platform_token" "test" { | ||
identifier = "test_token" | ||
name = "test token" | ||
account_id = "account_id" | ||
org_id = "org_id" | ||
project_id = "project_id" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `account_id` (String) Account Identifier for the Entity | ||
- `identifier` (String) Unique identifier of the resource. | ||
- `name` (String) Name of the resource. | ||
|
||
### Optional | ||
|
||
- `token_status` (String) Status of Delegate Token (ACTIVE or REVOKED). If left empty both active and revoked tokens will be assumed. | ||
- `created_at` (String) Time when the delegate token is created. | ||
- `created_by` (String) created by details. | ||
- `org_id` (String) Unique identifier of the organization. | ||
- `project_id` (String) Unique identifier of the project. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `value` (String, Sensitive) Value of the delegate Token | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
# Import account level delegate token | ||
terraform import harness_platform_delegatetoken <token_id> | ||
|
||
# Import org level delegate token | ||
terraform import harness_platform_delegatetoken <org_id>/<token_id> | ||
|
||
# Import project level delegate token | ||
terraform import harness_platform_delegatetoken <org_id>/<project_id>/<token_id> | ||
``` |
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
101 changes: 101 additions & 0 deletions
101
internal/service/platform/delegate_token/data_source_delegateToken.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,101 @@ | ||
package delegatetoken | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/harness/harness-go-sdk/harness/nextgen" | ||
"github.com/harness/terraform-provider-harness/helpers" | ||
"github.com/harness/terraform-provider-harness/internal" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func DataSourceDelegateToken() *schema.Resource { | ||
resource := &schema.Resource{ | ||
Description: "Data source for retrieving a Harness delegate Token.", | ||
|
||
ReadContext: dataSourceDelegateTokenRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Description: "Name of the delegate token", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"account_id": { | ||
Description: "Account Identifier for the Entity", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"org_id": { | ||
Description: "Org Identifier for the Entity", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"project_id": { | ||
Description: "Project Identifier for the Entity", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
RequiredWith: []string{"org_id"}, | ||
}, | ||
"token_status": { | ||
Description: "Status of Delegate Token (ACTIVE or REVOKED). If left empty both active and revoked tokens will be assumed", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "REVOKED"}, false), | ||
}, | ||
"value": { | ||
Description: "Value of the delegate token. Encoded in base64.", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"created_at": { | ||
Description: "Time when the delegate token is created. This is an epoch timestamp.", | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
|
||
return resource | ||
} | ||
|
||
func dataSourceDelegateTokenRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
var delegateToken *nextgen.DelegateTokenDetails | ||
|
||
name := d.Get("name").(string) | ||
|
||
if name != "" { | ||
var err error | ||
var httpResp *http.Response | ||
resp, httpResp, err := c.DelegateTokenResourceApi.GetDelegateTokens(ctx, c.AccountId, &nextgen.DelegateTokenResourceApiGetDelegateTokensOpts{ | ||
OrgIdentifier: helpers.BuildField(d, "org_id"), | ||
ProjectIdentifier: helpers.BuildField(d, "project_id"), | ||
Name: helpers.BuildField(d, "name"), | ||
Status: helpers.BuildField(d, "token_status"), | ||
}) | ||
if err != nil { | ||
return helpers.HandleApiError(err, d, httpResp) | ||
} | ||
if resp.Resource != nil { | ||
delegateToken = &resp.Resource[0] | ||
} | ||
|
||
if delegateToken == nil { | ||
d.SetId("") | ||
d.MarkNewResource() | ||
return nil | ||
} | ||
} else { | ||
return diag.FromErr(errors.New("Name must be specified")) | ||
} | ||
|
||
readDelegateToken(d, delegateToken) | ||
|
||
return nil | ||
} |
145 changes: 145 additions & 0 deletions
145
internal/service/platform/delegate_token/data_source_delegateToken_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,145 @@ | ||
package delegatetoken_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"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 TestAccDataSourceDelegateToken(t *testing.T) { | ||
name := utils.RandStringBytes(5) | ||
account_id := os.Getenv("HARNESS_ACCOUNT_ID") | ||
|
||
resourceName := "data.harness_platform_delegatetoken.test" | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.TestAccPreCheck(t) }, | ||
ProviderFactories: acctest.ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: tesAccDataSourceDelegateToken(name, account_id), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", name), | ||
resource.TestCheckResourceAttr(resourceName, "token_status", "ACTIVE"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceDelegateTokenOrgLevel(t *testing.T) { | ||
name := utils.RandStringBytes(5) | ||
account_id := os.Getenv("HARNESS_ACCOUNT_ID") | ||
|
||
resourceName := "data.harness_platform_delegatetoken.test" | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.TestAccPreCheck(t) }, | ||
ProviderFactories: acctest.ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: tesAccDataSourceDelegateTokenOrgLevel(name, account_id), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", name), | ||
resource.TestCheckResourceAttr(resourceName, "token_status", "ACTIVE"), | ||
resource.TestCheckResourceAttr(resourceName, "org_id", name), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceDelegateTokenProjectLevel(t *testing.T) { | ||
name := utils.RandStringBytes(5) | ||
account_id := os.Getenv("HARNESS_ACCOUNT_ID") | ||
|
||
resourceName := "data.harness_platform_delegatetoken.test" | ||
|
||
resource.UnitTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.TestAccPreCheck(t) }, | ||
ProviderFactories: acctest.ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: tesAccDataSourceDelegateTokenProjectLevel(name, account_id), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", name), | ||
resource.TestCheckResourceAttr(resourceName, "token_status", "ACTIVE"), | ||
resource.TestCheckResourceAttr(resourceName, "org_id", name), | ||
resource.TestCheckResourceAttr(resourceName, "project_id", name), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func tesAccDataSourceDelegateToken(name string, accountId string) string { | ||
return fmt.Sprintf(` | ||
resource "harness_platform_delegatetoken" "test" { | ||
identifier = "%[1]s" | ||
name = "%[1]s" | ||
account_id = "%[2]s" | ||
} | ||
data "harness_platform_delegatetoken" "test" { | ||
name = harness_platform_delegatetoken.test.name | ||
account_id = harness_platform_delegatetoken.test.account_id | ||
} | ||
`, name, accountId) | ||
} | ||
|
||
func tesAccDataSourceDelegateTokenOrgLevel(name string, accountId string) string { | ||
return fmt.Sprintf(` | ||
resource "harness_platform_organization" "test" { | ||
identifier = "%[1]s" | ||
name = "%[1]s" | ||
} | ||
resource "harness_platform_delegatetoken" "test" { | ||
identifier = "%[1]s" | ||
name = "%[1]s" | ||
account_id = "%[2]s" | ||
org_id = harness_platform_organization.test.id | ||
} | ||
data "harness_platform_delegatetoken" "test" { | ||
name = harness_platform_delegatetoken.test.name | ||
account_id = harness_platform_delegatetoken.test.account_id | ||
org_id = harness_platform_delegatetoken.test.org_id | ||
} | ||
`, name, accountId) | ||
} | ||
|
||
func tesAccDataSourceDelegateTokenProjectLevel(name string, accountId string) string { | ||
return fmt.Sprintf(` | ||
resource "harness_platform_organization" "test" { | ||
identifier = "%[1]s" | ||
name = "%[1]s" | ||
} | ||
resource "harness_platform_project" "test" { | ||
identifier = "%[1]s" | ||
name = "%[1]s" | ||
org_id = harness_platform_organization.test.id | ||
color = "#472848" | ||
} | ||
resource "harness_platform_delegatetoken" "test" { | ||
identifier = "%[1]s" | ||
name = "%[1]s" | ||
account_id = "%[2]s" | ||
org_id = harness_platform_organization.test.id | ||
project_id = harness_platform_project.test.id | ||
} | ||
data "harness_platform_delegatetoken" "test" { | ||
name = harness_platform_delegatetoken.test.name | ||
account_id = harness_platform_delegatetoken.test.account_id | ||
org_id = harness_platform_delegatetoken.test.org_id | ||
project_id = harness_platform_delegatetoken.test.project_id | ||
} | ||
`, name, accountId) | ||
} |
Oops, something went wrong.