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

Add Api Tokens Data Source #104

Merged
merged 34 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e4bd1d5
setup api token data source
ichung08 Jul 8, 2024
d951f3f
add to provider and fix descriptions
ichung08 Jul 8, 2024
90b2b4d
fix schema and add api tokens to provider
ichung08 Jul 8, 2024
ed8e5e8
add tests
ichung08 Jul 8, 2024
38d6a25
fix test
ichung08 Jul 8, 2024
7004e92
Update examples/data-sources/astro_api_tokens/data-source.tf
ichung08 Jul 9, 2024
8432791
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
2e4031e
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
0ff3bdb
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
9e634d1
address comments
ichung08 Jul 9, 2024
a606068
fix test
ichung08 Jul 9, 2024
00f7179
fix test
ichung08 Jul 9, 2024
1f5bd10
fix test
ichung08 Jul 9, 2024
584b2c7
fix test
ichung08 Jul 9, 2024
295cea4
fix test
ichung08 Jul 9, 2024
2cc0da3
fix test
ichung08 Jul 9, 2024
e01842a
fix test
ichung08 Jul 9, 2024
781c9c4
fix test
ichung08 Jul 9, 2024
c2c0c86
fix test
ichung08 Jul 9, 2024
c53a75c
fix test
ichung08 Jul 9, 2024
09c4824
Update internal/provider/schemas/api_token.go
ichung08 Jul 9, 2024
71b3ce8
Update internal/provider/schemas/api_token.go
ichung08 Jul 9, 2024
512ee36
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
3c221fc
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
b6e1f13
Update internal/provider/schemas/role.go
ichung08 Jul 9, 2024
cb9cd29
Update internal/provider/datasources/data_source_api_tokens_test.go
ichung08 Jul 9, 2024
5d30bf6
Update internal/provider/datasources/data_source_api_tokens_test.go
ichung08 Jul 9, 2024
470d8d5
address comments
ichung08 Jul 9, 2024
c1c8049
Merge branch '103-add-api-tokens-data-source' of github.com:astronome…
ichung08 Jul 9, 2024
314065b
fix tests
ichung08 Jul 9, 2024
7f687ca
address comments
ichung08 Jul 9, 2024
86691c5
fix
ichung08 Jul 9, 2024
04b2057
fix
ichung08 Jul 9, 2024
a3a3a9b
fix deployment test
ichung08 Jul 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 18 additions & 33 deletions internal/provider/datasources/data_source_api_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import (
)

type checkApiTokensInput struct {
filterWorkspaceId bool
filterDeploymentId bool
filterOrgOnly bool
workspaceId string
deploymentId string
organizationId string
workspaceId string
deploymentId string
organizationId string
}

func TestAcc_DataSourceApiTokens(t *testing.T) {
Expand All @@ -38,51 +35,39 @@ func TestAcc_DataSourceApiTokens(t *testing.T) {
Config: astronomerprovider.ProviderConfig(t, true) + apiTokens(tfVarName),
Check: resource.ComposeTestCheckFunc(
checkApiTokens(tfVarName, checkApiTokensInput{
filterWorkspaceId: false,
filterDeploymentId: false,
filterOrgOnly: false,
workspaceId: "",
deploymentId: "",
organizationId: tfOrganizationId,
workspaceId: "",
deploymentId: "",
organizationId: "",
}),
),
},
{
Config: astronomerprovider.ProviderConfig(t, true) + apiTokensFilterWorkspaceId(tfVarName, tfWorkspaceId),
Check: resource.ComposeTestCheckFunc(
checkApiTokens(tfVarName, checkApiTokensInput{
filterWorkspaceId: true,
filterDeploymentId: false,
filterOrgOnly: false,
workspaceId: tfWorkspaceId,
deploymentId: "",
organizationId: tfOrganizationId,
workspaceId: tfWorkspaceId,
deploymentId: "",
organizationId: "",
}),
),
},
{
Config: astronomerprovider.ProviderConfig(t, true) + apiTokensFilterDeploymentId(tfVarName, tfDeploymentId),
Check: resource.ComposeTestCheckFunc(
checkApiTokens(tfVarName, checkApiTokensInput{
filterWorkspaceId: false,
filterDeploymentId: true,
filterOrgOnly: false,
workspaceId: "",
deploymentId: tfDeploymentId,
organizationId: tfOrganizationId,
workspaceId: "",
deploymentId: tfDeploymentId,
organizationId: "",
}),
),
},
{
Config: astronomerprovider.ProviderConfig(t, true) + apiTokensFilterOrgOnly(tfVarName),
Check: resource.ComposeTestCheckFunc(
checkApiTokens(tfVarName, checkApiTokensInput{
filterWorkspaceId: false,
filterDeploymentId: false,
filterOrgOnly: true,
workspaceId: "",
deploymentId: "",
organizationId: tfOrganizationId,
workspaceId: "",
deploymentId: "",
organizationId: tfOrganizationId,
}),
),
},
Expand Down Expand Up @@ -170,7 +155,7 @@ func checkApiTokens(tfVarName string, input checkApiTokensInput) resource.TestCh
entityTypeKey := fmt.Sprintf("api_tokens.%d.roles.0.entity_type", apiTokensIdx)
entityType := instanceState.Attributes[entityTypeKey]
role := fmt.Sprintf("api_tokens.%d.roles.0.role", apiTokensIdx)
if input.filterWorkspaceId {
if len(input.workspaceId) > 0 {
if entityType != string(iam.ApiTokenRoleEntityTypeWORKSPACE) {
return fmt.Errorf("expected 'entity_type' to be set to 'workspace'")
}
Expand All @@ -182,7 +167,7 @@ func checkApiTokens(tfVarName string, input checkApiTokensInput) resource.TestCh
}
}

if input.filterDeploymentId {
if len(input.deploymentId) > 0 {
if entityType != string(iam.ApiTokenRoleEntityTypeDEPLOYMENT) {
return fmt.Errorf("expected 'entity_type' to be set to 'deployment'")
}
Expand All @@ -191,7 +176,7 @@ func checkApiTokens(tfVarName string, input checkApiTokensInput) resource.TestCh
}
}

if input.filterOrgOnly {
if len(input.organizationId) > 0 {
if entityType != string(iam.ApiTokenRoleEntityTypeORGANIZATION) {
return fmt.Errorf("expected 'entity_type' to be set to 'organization'")
}
Expand Down
11 changes: 6 additions & 5 deletions internal/utils/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"strings"

"github.com/samber/lo"

"github.com/astronomer/terraform-provider-astro/internal/clients/iam"

"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down Expand Up @@ -111,16 +113,15 @@ func CheckRole(role string, scopeType string) bool {
var WorkspaceRoles = []string{string(iam.WORKSPACEACCESSOR), string(iam.WORKSPACEAUTHOR), string(iam.WORKSPACEMEMBER), string(iam.WORKSPACEOWNER), string(iam.WORKSPACEOPERATOR)}
var roles []string

scopeType = strings.ToLower(scopeType)
if scopeType == "organization" {
roles = OrganizationRoles
} else if scopeType == "workspace" {
roles = WorkspaceRoles
} else if scopeType == "deployment" {
return true
}

for _, r := range roles {
if r == role {
return true
}
}
lo.Contains(roles, role)
return false
ichung08 marked this conversation as resolved.
Show resolved Hide resolved
}