-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add new OIDC credentials creation and assigment (Azure data sou…
…rce) (#773)
- Loading branch information
1 parent
7ad25a2
commit fcf27bc
Showing
9 changed files
with
200 additions
and
72 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,119 @@ | ||
package env0 | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/env0/terraform-provider-env0/client/http" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAzureOidcCredentialDataSource(t *testing.T) { | ||
credentials := client.Credentials{ | ||
Id: "id0", | ||
Name: "name0", | ||
Type: string(client.AzureOidcCredentialsType), | ||
} | ||
|
||
credentialsOther1 := client.Credentials{ | ||
Id: "id1", | ||
Name: "name1", | ||
Type: string(client.AzureOidcCredentialsType), | ||
} | ||
|
||
credentialsOther2 := client.Credentials{ | ||
Id: "id2", | ||
Name: "name2", | ||
Type: string(client.AzureServicePrincipalCredentialsType), | ||
} | ||
|
||
byName := map[string]interface{}{"name": credentials.Name} | ||
byId := map[string]interface{}{"id": credentials.Id} | ||
|
||
resourceType := "env0_azure_oidc_credentials" | ||
resourceName := "test_azure_oidc_credentials" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
getValidTestCase := func(input map[string]interface{}) resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, input), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", credentials.Id), | ||
resource.TestCheckResourceAttr(accessor, "name", credentials.Name), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
getErrorTestCase := func(input map[string]interface{}, expectedError string) resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, input), | ||
ExpectError: regexp.MustCompile(expectedError), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
mockGetCredentials := func(returnValue client.Credentials) func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().CloudCredentials(credentials.Id).AnyTimes().Return(returnValue, nil) | ||
} | ||
} | ||
|
||
mockListCredentials := func(returnValue []client.Credentials) func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().CloudCredentialsList().AnyTimes().Return(returnValue, nil) | ||
} | ||
} | ||
|
||
t.Run("by id", func(t *testing.T) { | ||
runUnitTest(t, | ||
getValidTestCase(byId), | ||
mockGetCredentials(credentials), | ||
) | ||
}) | ||
|
||
t.Run("by name", func(t *testing.T) { | ||
runUnitTest(t, | ||
getValidTestCase(byName), | ||
mockListCredentials([]client.Credentials{credentials, credentialsOther1, credentialsOther2}), | ||
) | ||
}) | ||
|
||
t.Run("throw error when no name or id is supplied", func(t *testing.T) { | ||
runUnitTest(t, | ||
getErrorTestCase(map[string]interface{}{}, "one of `id,name` must be specified"), | ||
func(mock *client.MockApiClientInterface) {}, | ||
) | ||
}) | ||
|
||
t.Run("throw error when by name and more than one is returned", func(t *testing.T) { | ||
runUnitTest(t, | ||
getErrorTestCase(byName, "found multiple credentials"), | ||
mockListCredentials([]client.Credentials{credentials, credentialsOther1, credentialsOther2, credentials}), | ||
) | ||
}) | ||
|
||
t.Run("Throw error when by name and not found", func(t *testing.T) { | ||
runUnitTest(t, | ||
getErrorTestCase(byName, "not found"), | ||
mockListCredentials([]client.Credentials{credentialsOther1, credentialsOther2}), | ||
) | ||
}) | ||
|
||
t.Run("Throw error when by id and not found", func(t *testing.T) { | ||
runUnitTest(t, | ||
getErrorTestCase(byId, fmt.Sprintf("id %s not found", credentials.Id)), | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().CloudCredentials(credentials.Id).AnyTimes().Return(client.Credentials{}, http.NewMockFailedResponseError(404)) | ||
}, | ||
) | ||
}) | ||
} |
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,55 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataOidcCredentials(credentialsType CloudType) *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataOidcCredentialRead(credentialsType), | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: fmt.Sprintf("the name of the %s oidc credentials", credentialsType), | ||
Optional: true, | ||
ExactlyOneOf: []string{"name", "id"}, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: fmt.Sprintf("the id of the %s oidc credentials", credentialsType), | ||
Optional: true, | ||
ExactlyOneOf: []string{"name", "id"}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataOidcCredentialRead(credentialsType CloudType) func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var credentials client.Credentials | ||
var err error | ||
|
||
id, ok := d.GetOk("id") | ||
if ok { | ||
credentials, err = getCredentialsById(id.(string), credentialsTypeToPrefixList[credentialsType], meta) | ||
} else { | ||
credentials, err = getCredentialsByName(d.Get("name").(string), credentialsTypeToPrefixList[credentialsType], meta) | ||
} | ||
|
||
if err != nil { | ||
return DataGetFailure(fmt.Sprintf("%s oidc credentials", credentialsType), id, err) | ||
} | ||
|
||
if err := writeResourceData(&credentials, d); err != nil { | ||
return diag.Errorf("schema resource data serialization failed: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
} |
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
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
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
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
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