-
Notifications
You must be signed in to change notification settings - Fork 42
Feat/add custom role resource and data source #1077
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
Open
Markus-Schwer
wants to merge
1
commit into
stackitcloud:main
Choose a base branch
from
Markus-Schwer:feat/add-custom-role-resource-and-data-source
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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
158 changes: 158 additions & 0 deletions
158
stackit/internal/services/authorization/customrole/datasource.go
This file contains hidden or 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,158 @@ | ||
| package customrole | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/authorization" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||
| authorizationUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/authorization/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||
| ) | ||
|
|
||
| // Ensure the implementation satisfies the expected interfaces. | ||
| var ( | ||
| _ datasource.DataSource = &customRoleDataSource{} | ||
| ) | ||
|
|
||
| // NewAuthorizationDataSource creates a new customrole of the authorizationDataSource. | ||
| func NewCustomRoleDataSource() datasource.DataSource { | ||
| return &customRoleDataSource{} | ||
| } | ||
|
|
||
| // NewProjectRoleAssignmentDataSources is a helper function generate custom role | ||
| // data sources for all possible resource types. | ||
| func NewCustomRoleDataSources() []func() datasource.DataSource { | ||
| resources := make([]func() datasource.DataSource, 0) | ||
| for _, v := range resourceTypes { | ||
| resources = append(resources, func() datasource.DataSource { | ||
| return &customRoleDataSource{ | ||
| resourceType: v, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| return resources | ||
| } | ||
|
|
||
| // customRoleDataSource is the datasource implementation. | ||
| type customRoleDataSource struct { | ||
| resourceType string | ||
| client *authorization.APIClient | ||
| } | ||
|
|
||
| // Configure sets up the API client for the authorization customrole resource. | ||
| func (d *customRoleDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
| providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| apiClient := authorizationUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) | ||
|
|
||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| d.client = apiClient | ||
|
|
||
| tflog.Info(ctx, "authorization client configured") | ||
| } | ||
|
|
||
| // Metadata provides metadata for the custom role datasource. | ||
| func (d *customRoleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| resp.TypeName = fmt.Sprintf("%s_authorization_%s_custom_role", req.ProviderTypeName, d.resourceType) | ||
| } | ||
|
|
||
| // Schema defines the schema for the custom role data source. | ||
| func (d *customRoleDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
| resp.Schema = schema.Schema{ | ||
| Description: descriptions["main"], | ||
| Attributes: map[string]schema.Attribute{ | ||
| "id": schema.StringAttribute{ | ||
| Description: descriptions["id"], | ||
| Computed: true, | ||
| }, | ||
| "role_id": schema.StringAttribute{ | ||
| Description: descriptions["role_id"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "resource_id": schema.StringAttribute{ | ||
| Description: descriptions["resource_id"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "name": schema.StringAttribute{ | ||
| Description: descriptions["name"], | ||
| Computed: true, | ||
| }, | ||
| "description": schema.StringAttribute{ | ||
| Description: descriptions["subject"], | ||
| Computed: true, | ||
| }, | ||
| "permissions": schema.ListAttribute{ | ||
| ElementType: types.StringType, | ||
| Description: descriptions["permissions"], | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (d *customRoleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { //nolint:gocritic // function signature required by Terraform | ||
| var model Model | ||
| diags := req.Config.Get(ctx, &model) | ||
| resp.Diagnostics.Append(diags...) | ||
|
|
||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| resourceId := model.ResourceId.ValueString() | ||
| roleId := model.RoleId.ValueString() | ||
|
|
||
| roleResp, err := d.client.GetRole(ctx, d.resourceType, resourceId, roleId).Execute() | ||
| if err != nil { | ||
| var oapiErr *oapierror.GenericOpenAPIError | ||
|
|
||
| ok := errors.As(err, &oapiErr) | ||
| if ok && oapiErr.StatusCode == http.StatusNotFound { | ||
| resp.State.RemoveResource(ctx) | ||
| return | ||
| } | ||
|
|
||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading custom role", fmt.Sprintf("Calling API: %v", err)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if err = mapGetCustomRoleResponse(ctx, roleResp, &model); err != nil { | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading custom role", fmt.Sprintf("Processing API response: %v", err)) | ||
| return | ||
| } | ||
|
|
||
| // Set the updated state. | ||
| diags = resp.State.Set(ctx, &model) | ||
| resp.Diagnostics.Append(diags...) | ||
| tflog.Info(ctx, fmt.Sprintf("read custom role %s", roleId)) | ||
| } |
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.
internal SDK modules shouldn't be used
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.
Ah, I forgot to remove it after testing...