Skip to content

Commit

Permalink
fix: [PL-39175]: PR for adding delegate token resource (#719)
Browse files Browse the repository at this point in the history
* 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
VikasMaddukuriHarness committed Oct 26, 2023
1 parent 7dcb881 commit e72b2a9
Show file tree
Hide file tree
Showing 8 changed files with 755 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/719.txt
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.
```
45 changes: 45 additions & 0 deletions docs/data-sources/platform_delegate_token.md
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
76 changes: 76 additions & 0 deletions docs/resources/platform_delegate_token.md
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>
```
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/harness/terraform-provider-harness/internal/service/platform/ccm_filters"
"github.com/harness/terraform-provider-harness/internal/service/platform/connector"
pl_current_user "github.com/harness/terraform-provider-harness/internal/service/platform/current_user"
pl_delegatetoken "github.com/harness/terraform-provider-harness/internal/service/platform/delegate_token"
pl_environment "github.com/harness/terraform-provider-harness/internal/service/platform/environment"
pl_environment_clusters_mapping "github.com/harness/terraform-provider-harness/internal/service/platform/environment_clusters_mapping"
pl_environment_group "github.com/harness/terraform-provider-harness/internal/service/platform/environment_group"
Expand Down Expand Up @@ -243,6 +244,7 @@ func Provider(version string) func() *schema.Provider {
"harness_autostopping_aws_alb": load_balancer.DataSourceAwsALB(),
"harness_autostopping_azure_gateway": load_balancer.DataSourceAzureGateway(),
"harness_autostopping_schedule": schedule.DataSourceFixedSchedule(),
"harness_platform_delegatetoken": pl_delegatetoken.DataSourceDelegateToken(),
},
ResourcesMap: map[string]*schema.Resource{
"harness_platform_template": pl_template.ResourceTemplate(),
Expand Down Expand Up @@ -370,6 +372,7 @@ func Provider(version string) func() *schema.Provider {
"harness_autostopping_aws_alb": load_balancer.ResourceAwsALB(),
"harness_autostopping_azure_gateway": load_balancer.ResourceAzureGateway(),
"harness_autostopping_schedule": schedule.ResourceVMRule(),
"harness_platform_delegatetoken": pl_delegatetoken.ResourceDelegateToken(),
},
}

Expand Down
101 changes: 101 additions & 0 deletions internal/service/platform/delegate_token/data_source_delegateToken.go
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
}
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)
}
Loading

0 comments on commit e72b2a9

Please sign in to comment.