-
Notifications
You must be signed in to change notification settings - Fork 67
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
feat: Add support for generic oidc accounts #831
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0ad741a
feat: Add support for generic oidc accounts
grace-rehn 633ad4b
chore: remove subject keys which are not needed
grace-rehn 53d4eb6
Use go client branch for now and update docs
grace-rehn 0b44e43
fix: typos
grace-rehn 33358ae
fix: more typos and unused fields
grace-rehn a6035b8
fix: move implementation to framework
grace-rehn adb50a0
chore: doc updates
grace-rehn 6f35e56
fix: docs
grace-rehn 2d9538e
fix: docs again
grace-rehn e7d2878
fix: docs again
grace-rehn c41edd6
chore: update go client version
grace-rehn c7e432c
chore: add addition test step to test update
grace-rehn 7e2e2a2
Merge branch 'main' into grace/generic-oidc-accounts
grace-rehn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,51 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "octopusdeploy_generic_oidc_account Resource - terraform-provider-octopusdeploy" | ||
subcategory: "" | ||
description: |- | ||
This resource manages a Generic OIDC Account in Octopus Deploy. | ||
--- | ||
|
||
# octopusdeploy_generic_oidc_account (Resource) | ||
|
||
This resource manages a Generic OIDC Account in Octopus Deploy. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "octopusdeploy_generic_oidc_account" "example" { | ||
name = "Generic OpenID Connect Account (OK to Delete)" | ||
execution_subject_keys = ["space", "project"] | ||
audience = "api://default" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the generic oidc account. | ||
|
||
### Optional | ||
|
||
- `audience` (String) The audience associated with this resource. | ||
- `description` (String) The description of this generic oidc account. | ||
- `environments` (List of String) A list of environment IDs associated with this resource. | ||
- `execution_subject_keys` (List of String) Keys to include in a deployment or runbook. Valid options are `space`, `environment`, `project`, `tenant`, `runbook`, `account`, `type`. | ||
- `space_id` (String) The space ID associated with this resource. | ||
- `tenant_tags` (List of String) A list of tenant tags associated with this resource. | ||
- `tenanted_deployment_participation` (String) The tenanted deployment mode of the resource. Valid account types are `Untenanted`, `TenantedOrUntenanted`, or `Tenanted`. | ||
- `tenants` (List of String) A list of tenant IDs associated with this resource. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The unique ID for this resource. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
terraform import [options] octopusdeploy_generic_oidc_account.<name> <account-id> | ||
``` |
1 change: 1 addition & 0 deletions
1
examples/resources/octopusdeploy_generic_oidc_account/import.sh
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 @@ | ||
terraform import [options] octopusdeploy_generic_oidc_account.<name> <account-id> |
5 changes: 5 additions & 0 deletions
5
examples/resources/octopusdeploy_generic_oidc_account/resource.tf
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,5 @@ | ||
resource "octopusdeploy_generic_oidc_account" "example" { | ||
name = "Generic OpenID Connect Account (OK to Delete)" | ||
execution_subject_keys = ["space", "project"] | ||
audience = "api://default" | ||
} |
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
179 changes: 179 additions & 0 deletions
179
octopusdeploy_framework/resource_generic_oidc_account.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,179 @@ | ||
package octopusdeploy_framework | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/accounts" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas" | ||
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
var _ resource.Resource = &genericOidcAccountResource{} | ||
var _ resource.ResourceWithImportState = &genericOidcAccountResource{} | ||
|
||
type genericOidcAccountResource struct { | ||
*Config | ||
} | ||
|
||
func NewGenericOidcResource() resource.Resource { | ||
return &genericOidcAccountResource{} | ||
} | ||
|
||
func (r *genericOidcAccountResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = util.GetTypeName("generic_oidc_account") | ||
} | ||
|
||
func (r *genericOidcAccountResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schemas.GenericOidcAccountSchema{}.GetResourceSchema() | ||
} | ||
|
||
func (r *genericOidcAccountResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
r.Config = ResourceConfiguration(req, resp) | ||
} | ||
func (r *genericOidcAccountResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var plan schemas.GenericOidcAccountResourceModel | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
tflog.Debug(ctx, "Creating generic oidc account", map[string]interface{}{ | ||
"name": plan.Name.ValueString(), | ||
}) | ||
|
||
account := expandGenericOidcAccountResource(ctx, plan) | ||
createdAccount, err := accounts.Add(r.Client, account) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error creating generic oidc account", err.Error()) | ||
return | ||
} | ||
|
||
state := flattenGenericOidcAccountResource(ctx, createdAccount.(*accounts.GenericOIDCAccount), plan) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...) | ||
} | ||
|
||
func (r *genericOidcAccountResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var state schemas.GenericOidcAccountResourceModel | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
account, err := accounts.GetByID(r.Client, state.SpaceID.ValueString(), state.ID.ValueString()) | ||
if err != nil { | ||
if err := errors.ProcessApiErrorV2(ctx, resp, state, err, "genericOidcAccountResource"); err != nil { | ||
resp.Diagnostics.AddError("unable to load generic oidc account", err.Error()) | ||
} | ||
return | ||
} | ||
|
||
newState := flattenGenericOidcAccountResource(ctx, account.(*accounts.GenericOIDCAccount), state) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) | ||
} | ||
|
||
func (r *genericOidcAccountResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var plan schemas.GenericOidcAccountResourceModel | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
account := expandGenericOidcAccountResource(ctx, plan) | ||
updatedAccount, err := accounts.Update(r.Client, account) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error updating generic oidc account", err.Error()) | ||
return | ||
} | ||
|
||
state := flattenGenericOidcAccountResource(ctx, updatedAccount.(*accounts.GenericOIDCAccount), plan) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...) | ||
} | ||
|
||
func (r *genericOidcAccountResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var state schemas.GenericOidcAccountResourceModel | ||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
err := accounts.DeleteByID(r.Client, state.SpaceID.ValueString(), state.ID.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Error deleting generic oidc account", err.Error()) | ||
return | ||
} | ||
} | ||
|
||
func (r *genericOidcAccountResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
accountID := req.ID | ||
|
||
account, err := accounts.GetByID(r.Client, r.Client.GetSpaceID(), accountID) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Error reading generic oidc account", | ||
fmt.Sprintf("Unable to read generic oidc account with ID %s: %s", accountID, err.Error()), | ||
) | ||
return | ||
} | ||
|
||
genericOidcAccount, ok := account.(*accounts.GenericOIDCAccount) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected account type", | ||
fmt.Sprintf("Expected generic oidc account, got: %T", account), | ||
) | ||
return | ||
} | ||
|
||
state := schemas.GenericOidcAccountResourceModel{ | ||
SpaceID: types.StringValue(genericOidcAccount.GetSpaceID()), | ||
Name: types.StringValue(genericOidcAccount.GetName()), | ||
Description: types.StringValue(genericOidcAccount.GetDescription()), | ||
TenantedDeploymentParticipation: types.StringValue(string(genericOidcAccount.GetTenantedDeploymentMode())), | ||
Environments: flattenStringList(genericOidcAccount.GetEnvironmentIDs(), types.ListNull(types.StringType)), | ||
Tenants: flattenStringList(genericOidcAccount.GetTenantIDs(), types.ListNull(types.StringType)), | ||
TenantTags: flattenStringList(genericOidcAccount.TenantTags, types.ListNull(types.StringType)), | ||
ExecutionSubjectKeys: flattenStringList(genericOidcAccount.DeploymentSubjectKeys, types.ListNull(types.StringType)), | ||
Audience: types.StringValue(genericOidcAccount.Audience), | ||
} | ||
state.ID = types.StringValue(genericOidcAccount.ID) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) | ||
} | ||
|
||
func expandGenericOidcAccountResource(ctx context.Context, model schemas.GenericOidcAccountResourceModel) *accounts.GenericOIDCAccount { | ||
account, _ := accounts.NewGenericOIDCAccount(model.Name.ValueString()) | ||
|
||
account.SetID(model.ID.ValueString()) | ||
account.SetDescription(model.Description.ValueString()) | ||
account.SetSpaceID(model.SpaceID.ValueString()) | ||
account.SetEnvironmentIDs(util.ExpandStringList(model.Environments)) | ||
account.SetTenantedDeploymentMode(core.TenantedDeploymentMode(model.TenantedDeploymentParticipation.ValueString())) | ||
account.SetTenantIDs(util.ExpandStringList(model.Tenants)) | ||
account.SetTenantTags(util.ExpandStringList(model.TenantTags)) | ||
account.DeploymentSubjectKeys = util.ExpandStringList(model.ExecutionSubjectKeys) | ||
account.Audience = model.Audience.ValueString() | ||
|
||
return account | ||
} | ||
|
||
func flattenGenericOidcAccountResource(ctx context.Context, account *accounts.GenericOIDCAccount, model schemas.GenericOidcAccountResourceModel) schemas.GenericOidcAccountResourceModel { | ||
model.ID = types.StringValue(account.GetID()) | ||
model.SpaceID = types.StringValue(account.GetSpaceID()) | ||
model.Name = types.StringValue(account.GetName()) | ||
model.Description = types.StringValue(account.GetDescription()) | ||
model.TenantedDeploymentParticipation = types.StringValue(string(account.GetTenantedDeploymentMode())) | ||
|
||
model.Environments = util.FlattenStringList(account.GetEnvironmentIDs()) | ||
model.Tenants = util.FlattenStringList(account.GetTenantIDs()) | ||
model.TenantTags = util.FlattenStringList(account.TenantTags) | ||
|
||
model.ExecutionSubjectKeys = util.FlattenStringList(account.DeploymentSubjectKeys) | ||
model.Audience = types.StringValue(account.Audience) | ||
|
||
return model | ||
} |
78 changes: 78 additions & 0 deletions
78
octopusdeploy_framework/resource_generic_oidc_account_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,78 @@ | ||
package octopusdeploy_framework | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestAccGenericOidcAccountBasic(t *testing.T) { | ||
localName := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
resourceName := "octopusdeploy_generic_oidc_account." + localName | ||
|
||
description := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
name := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
tenantedDeploymentParticipation := core.TenantedDeploymentModeTenantedOrUntenanted | ||
|
||
executionKeys := []string{"space"} | ||
audience := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
updatedAudience := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) | ||
|
||
config := testGenericOidcAccountBasic(localName, name, description, tenantedDeploymentParticipation, executionKeys, audience) | ||
updateConfig := testGenericOidcAccountBasic(localName, name, description, tenantedDeploymentParticipation, executionKeys, updatedAudience) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { TestAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: ProtoV6ProviderFactories(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccountExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "description", description), | ||
resource.TestCheckResourceAttrSet(resourceName, "id"), | ||
resource.TestCheckResourceAttr(resourceName, "name", name), | ||
resource.TestCheckResourceAttrSet(resourceName, "space_id"), | ||
resource.TestCheckResourceAttr(resourceName, "tenanted_deployment_participation", string(tenantedDeploymentParticipation)), | ||
resource.TestCheckResourceAttr(resourceName, "execution_subject_keys.0", executionKeys[0]), | ||
resource.TestCheckResourceAttr(resourceName, "audience", audience), | ||
), | ||
ResourceName: resourceName, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please include a 2nd |
||
{ | ||
Config: updateConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccountExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "description", description), | ||
resource.TestCheckResourceAttrSet(resourceName, "id"), | ||
resource.TestCheckResourceAttr(resourceName, "name", name), | ||
resource.TestCheckResourceAttrSet(resourceName, "space_id"), | ||
resource.TestCheckResourceAttr(resourceName, "tenanted_deployment_participation", string(tenantedDeploymentParticipation)), | ||
resource.TestCheckResourceAttr(resourceName, "execution_subject_keys.0", executionKeys[0]), | ||
resource.TestCheckResourceAttr(resourceName, "audience", updatedAudience), | ||
), | ||
ResourceName: resourceName, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testGenericOidcAccountBasic(localName string, name string, description string, tenantedDeploymentParticipation core.TenantedDeploymentMode, execution_subject_keys []string, audience string) string { | ||
|
||
execKeysStr := fmt.Sprintf(`["%s"]`, strings.Join(execution_subject_keys, `", "`)) | ||
|
||
return fmt.Sprintf(`resource "octopusdeploy_generic_oidc_account" "%s" { | ||
description = "%s" | ||
name = "%s" | ||
tenanted_deployment_participation = "%s" | ||
execution_subject_keys = %s | ||
audience = "%s" | ||
} | ||
|
||
data "octopusdeploy_accounts" "test" { | ||
ids = [octopusdeploy_generic_oidc_account.%s.id] | ||
}`, localName, description, name, tenantedDeploymentParticipation, execKeysStr, audience, localName) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine to leave this as is but in the future, we can now use
any
instead ofinterface{}
in Go. Makes the code easier to read.