Skip to content

Commit

Permalink
Fix state drift with optional attributes with no default
Browse files Browse the repository at this point in the history
Fix typo in test name
  • Loading branch information
alexhung committed Mar 26, 2024
1 parent 2ec0218 commit ec9e6c1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
16 changes: 12 additions & 4 deletions pkg/platform/resource_oidc_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ type odicConfigurationResourceModel struct {

type odicConfigurationAPIModel struct {
Name string `json:"name"`
Description string `json:"description"`
Description string `json:"description,omitempty"`
IssuerURL string `json:"issuer_url"`
ProviderType string `json:"provider_type"`
Audience string `json:"audience"`
Audience string `json:"audience,omitempty"`
}

func (r *odicConfigurationResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down Expand Up @@ -198,10 +198,18 @@ func (r *odicConfigurationResource) Read(ctx context.Context, req resource.ReadR
// Convert from the API data model to the Terraform data model
// and refresh any attribute values.
state.Name = types.StringValue(odicConfig.Name)

if len(odicConfig.Description) > 0 {
state.Description = types.StringValue(odicConfig.Description)
}

state.IssuerURL = types.StringValue(odicConfig.IssuerURL)
state.Audience = types.StringValue(odicConfig.Audience)

if len(odicConfig.Audience) > 0 {
state.Audience = types.StringValue(odicConfig.Audience)
}

state.ProviderType = types.StringValue(odicConfig.ProviderType)
state.Description = types.StringValue(odicConfig.Description)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
Expand Down
16 changes: 11 additions & 5 deletions pkg/platform/resource_oidc_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,34 @@ func TestAccOIDCConfiguration_full(t *testing.T) {
temp := `
resource "platform_oidc_configuration" "{{ .name }}" {
name = "{{ .name }}"
description = "Test description"
issuer_url = "{{ .issuerURL }}"
provider_type = "{{ .providerType }}"
audience = "{{ .audience }}"
}`

testData := map[string]string{
"name": configName,
"issuerURL": "https://tempurl.org",
"providerType": "generic",
"audience": "test-audience-1",
}

config := testutil.ExecuteTemplate(configName, temp, testData)

updatedTemp := `
resource "platform_oidc_configuration" "{{ .name }}" {
name = "{{ .name }}"
description = "Test Description"
issuer_url = "{{ .issuerURL }}"
provider_type = "{{ .providerType }}"
audience = "{{ .audience }}"
}`

updatedTestData := map[string]string{
"name": configName,
"issuerURL": "https://token.actions.githubusercontent.com/",
"providerType": "GitHub",
"audience": "test-audience-2",
}
updatedConfig := testutil.ExecuteTemplate(configName, temp, updatedTestData)
updatedConfig := testutil.ExecuteTemplate(configName, updatedTemp, updatedTestData)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -47,13 +53,13 @@ func TestAccOIDCConfiguration_full(t *testing.T) {
resource.TestCheckResourceAttr(fqrn, "name", testData["name"]),
resource.TestCheckResourceAttr(fqrn, "issuer_url", testData["issuerURL"]),
resource.TestCheckResourceAttr(fqrn, "provider_type", testData["providerType"]),
resource.TestCheckResourceAttr(fqrn, "audience", testData["audience"]),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "name", updatedTestData["name"]),
resource.TestCheckResourceAttr(fqrn, "description", "Test Description"),
resource.TestCheckResourceAttr(fqrn, "issuer_url", updatedTestData["issuerURL"]),
resource.TestCheckResourceAttr(fqrn, "provider_type", updatedTestData["providerType"]),
resource.TestCheckResourceAttr(fqrn, "audience", updatedTestData["audience"]),
Expand Down
10 changes: 8 additions & 2 deletions pkg/platform/resource_oidc_identity_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ func (r *odicIdentityMappingResourceModel) toAPIModel(ctx context.Context, apiMo

func (r *odicIdentityMappingResourceModel) fromAPIModel(ctx context.Context, apiModel *odicIdentityMappingAPIModel) (ds diag.Diagnostics) {
r.Name = types.StringValue(apiModel.Name)
r.Description = types.StringValue(apiModel.Description)

if len(apiModel.Description) > 0 {
r.Description = types.StringValue(apiModel.Description)
}

r.Priority = types.Int64Value(apiModel.Priority)

claimsBytes, err := json.Marshal(apiModel.Claims)
Expand All @@ -198,12 +202,14 @@ func (r *odicIdentityMappingResourceModel) fromAPIModel(ctx context.Context, api

tokenSpecResource := odicIdentityMappingTokenSpecResourceModel{
Scope: types.StringValue(apiModel.TokenSpec.Scope),
Audience: types.StringValue(apiModel.TokenSpec.Audience),
ExpiresIn: types.Int64Value(apiModel.TokenSpec.ExpiresIn),
}
if len(apiModel.TokenSpec.Username) > 0 {
tokenSpecResource.Username = types.StringValue(apiModel.TokenSpec.Username)
}
if len(apiModel.TokenSpec.Audience) > 0 {
tokenSpecResource.Audience = types.StringValue(apiModel.TokenSpec.Audience)
}

tokenSpec, d := types.ObjectValueFrom(
ctx,
Expand Down
46 changes: 34 additions & 12 deletions pkg/platform/resource_oidc_identity_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ import (
"github.com/jfrog/terraform-provider-shared/testutil"
)

func TestAccOIDIdentityMapping_full(t *testing.T) {
func TestAccOIDCIdentityMapping_full(t *testing.T) {
_, _, configName := testutil.MkNames("test-oidc-configuration", "platform_oidc_configuration")
_, fqrn, identityMappingName := testutil.MkNames("test-oidc-identity-mapping", "platform_oidc_identity_mapping")

temp := `
resource "platform_oidc_configuration" "{{ .configName }}" {
name = "{{ .configName }}"
description = "Test description"
issuer_url = "{{ .issuerURL }}"
provider_type = "{{ .providerType }}"
audience = "{{ .audience }}"
}
resource "platform_oidc_identity_mapping" "{{ .identityMappingName }}" {
name = "{{ .identityMappingName }}"
description = "Test description"
provider_name = platform_oidc_configuration.{{ .configName }}.name
priority = {{ .priority }}
claims_json = jsonencode({
Expand All @@ -34,8 +32,6 @@ func TestAccOIDIdentityMapping_full(t *testing.T) {
token_spec = {
username = "{{ .username }}"
scope = "applied-permissions/user"
audience = "*@*"
expires_in = 120
}
}`

Expand All @@ -52,6 +48,31 @@ func TestAccOIDIdentityMapping_full(t *testing.T) {

config := testutil.ExecuteTemplate(identityMappingName, temp, testData)

updatedTemp := `
resource "platform_oidc_configuration" "{{ .configName }}" {
name = "{{ .configName }}"
issuer_url = "{{ .issuerURL }}"
provider_type = "{{ .providerType }}"
audience = "{{ .audience }}"
}
resource "platform_oidc_identity_mapping" "{{ .identityMappingName }}" {
name = "{{ .identityMappingName }}"
description = "Test description"
provider_name = platform_oidc_configuration.{{ .configName }}.name
priority = {{ .priority }}
claims_json = jsonencode({
sub = "{{ .sub }}",
updated_at = 1490198843
})
token_spec = {
username = "{{ .username }}"
scope = "applied-permissions/user"
audience = "jfrt@* jfac@* jfmc@* jfmd@* jfevt@* jfxfer@* jflnk@* jfint@* jfwks@*"
expires_in = 120
}
}`

updatedTestData := map[string]string{
"configName": configName,
"identityMappingName": identityMappingName,
Expand All @@ -63,7 +84,7 @@ func TestAccOIDIdentityMapping_full(t *testing.T) {
"username": fmt.Sprintf("test-user-%d", testutil.RandomInt()),
}

updatedConfig := testutil.ExecuteTemplate(identityMappingName, temp, updatedTestData)
updatedConfig := testutil.ExecuteTemplate(identityMappingName, updatedTemp, updatedTestData)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -78,18 +99,19 @@ func TestAccOIDIdentityMapping_full(t *testing.T) {
resource.TestCheckResourceAttr(fqrn, "token_spec.username", testData["username"]),
resource.TestCheckResourceAttr(fqrn, "token_spec.scope", "applied-permissions/user"),
resource.TestCheckResourceAttr(fqrn, "token_spec.audience", "*@*"),
resource.TestCheckResourceAttr(fqrn, "token_spec.expires_in", "120"),
resource.TestCheckResourceAttr(fqrn, "token_spec.expires_in", "60"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "name", updatedTestData["identityMappingName"]),
resource.TestCheckResourceAttr(fqrn, "description", "Test description"),
resource.TestCheckResourceAttr(fqrn, "priority", updatedTestData["priority"]),
resource.TestCheckResourceAttr(fqrn, "claims_json", fmt.Sprintf("{\"sub\":\"%s\",\"updated_at\":1490198843}", updatedTestData["sub"])),
resource.TestCheckResourceAttr(fqrn, "token_spec.username", updatedTestData["username"]),
resource.TestCheckResourceAttr(fqrn, "token_spec.scope", "applied-permissions/user"),
resource.TestCheckResourceAttr(fqrn, "token_spec.audience", "*@*"),
resource.TestCheckResourceAttr(fqrn, "token_spec.audience", "jfrt@* jfac@* jfmc@* jfmd@* jfevt@* jfxfer@* jflnk@* jfint@* jfwks@*"),
resource.TestCheckResourceAttr(fqrn, "token_spec.expires_in", "120"),
),
},
Expand All @@ -104,7 +126,7 @@ func TestAccOIDIdentityMapping_full(t *testing.T) {
})
}

func TestAccOIDIdentityMapping_groups_scope(t *testing.T) {
func TestAccOIDCIdentityMapping_groups_scope(t *testing.T) {
_, _, configName := testutil.MkNames("test-oidc-configuration", "platform_oidc_configuration")
_, fqrn, identityMappingName := testutil.MkNames("test-oidc-identity-mapping", "platform_oidc_identity_mapping")

Expand Down Expand Up @@ -165,7 +187,7 @@ func TestAccOIDIdentityMapping_groups_scope(t *testing.T) {
})
}

func TestAccOIDIdentityMapping_invalid_name(t *testing.T) {
func TestAccOIDCIdentityMapping_invalid_name(t *testing.T) {
for _, invalidName := range []string{"invalid name", "invalid!name"} {
t.Run(invalidName, func(t *testing.T) {
_, _, configName := testutil.MkNames("test-oidc-configuration", "platform_oidc_configuration")
Expand Down Expand Up @@ -224,7 +246,7 @@ func TestAccOIDIdentityMapping_invalid_name(t *testing.T) {
}
}

func TestAccOIDIdentityMapping_invalid_provider_name(t *testing.T) {
func TestAccOIDCIdentityMapping_invalid_provider_name(t *testing.T) {
for _, invalidName := range []string{"Test", "test!@", "1test"} {
t.Run(invalidName, func(t *testing.T) {
_, _, identityMappingName := testutil.MkNames("test-oidc-identity-mapping", "platform_oidc_identity_mapping")
Expand Down Expand Up @@ -272,7 +294,7 @@ func TestAccOIDIdentityMapping_invalid_provider_name(t *testing.T) {
}
}

func TestAccOIDIdentityMapping_invalid_scope(t *testing.T) {
func TestAccOIDCIdentityMapping_invalid_scope(t *testing.T) {
for _, invalidScope := range []string{"invalid-scope", "applied-permissions/group", "applied-permissions/groups"} {
t.Run(invalidScope, func(t *testing.T) {
_, _, configName := testutil.MkNames("test-oidc-configuration", "platform_oidc_configuration")
Expand Down

0 comments on commit ec9e6c1

Please sign in to comment.