-
Notifications
You must be signed in to change notification settings - Fork 28
first steps for kms key-ring resource and datasource #897
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
base: main
Are you sure you want to change the base?
Conversation
This PR was marked as stale after 7 days of inactivity and will be closed after another 7 days of further inactivity. If this PR should be kept open, just add a comment, remove the stale label or push new commits to it. |
panic("implement me") | ||
} | ||
|
||
func toCreatePayload(model *Model) (*kms.CreateKeyRingPayload, error) { |
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.
Please consider implementing a unit test for this func.
}, nil | ||
} | ||
|
||
func mapFields(keyRing *kms.KeyRing, model *Model) error { |
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.
Same here
return &keyRingResource{} | ||
} | ||
|
||
type keyRingResource struct { |
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.
Don't know if you are aware of this, please don't forget to implement the import.
You can ensure this by adding the code below:
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &keyRingResource{}
_ resource.ResourceWithConfigure = &keyRingResource{}
_ resource.ResourceWithImportState = &keyRingResource{}
)
See the git instance resource how to do it:
terraform-provider-stackit/stackit/internal/services/git/instance/resource.go
Lines 32 to 37 in 8e77675
// Ensure the implementation satisfies the expected interfaces. | |
var ( | |
_ resource.Resource = &gitResource{} | |
_ resource.ResourceWithConfigure = &gitResource{} | |
_ resource.ResourceWithImportState = &gitResource{} | |
) |
Consider doing the same for the datasource, see the git instance datasource for example:
} | ||
|
||
func (k *keyRingResource) Metadata(ctx context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { | ||
response.TypeName = request.ProviderTypeName + "kms_key_ring" |
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.
response.TypeName = request.ProviderTypeName + "kms_key_ring" | |
response.TypeName = request.ProviderTypeName + "_kms_key_ring" |
KeyRingId types.String `tfsdk:"key_ring_id"` | ||
Id types.String `tfsdk:"id"` // needed by TF | ||
ProjectId types.String `tfsdk:"project_id"` | ||
RegionId types.String `tfsdk:"region_id"` |
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.
RegionId types.String `tfsdk:"region_id"` | |
Region types.String `tfsdk:"region"` # small adjustment to stick with the naming conventions across the codebase |
According to https://docs.api.stackit.cloud/documentation/kms/version/v1beta , the KMS API already uses the new multi-region concept.
See e.g. the stackit_routing_table
resource how to implement the multi-region concept. The resource has a region
field which is marked as optional and will use the default_region
configured in the provider as a fallback: https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/resources/routing_table
- You will need to store the provider configuration within your resource struct:
terraform-provider-stackit/stackit/internal/services/iaasalpha/routingtable/table/resource.go
Line 63 in 8e77675
providerData core.ProviderData terraform-provider-stackit/stackit/internal/services/iaasalpha/routingtable/table/resource.go
Lines 73 to 77 in 8e77675
var ok bool r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { return } - Field definition:
terraform-provider-stackit/stackit/internal/services/iaasalpha/routingtable/table/resource.go
Lines 161 to 169 in 8e77675
"region": schema.StringAttribute{ Optional: true, // must be computed to allow for storing the override value from the provider Computed: true, Description: "The resource region. If not defined, the provider region is used.", PlanModifiers: []planmodifier.String{ stringplanmodifier.RequiresReplace(), }, }, - Then use this helper func to read the region:
terraform-provider-stackit/stackit/internal/services/iaasalpha/routingtable/table/resource.go
Line 203 in 8e77675
region := r.providerData.GetRegionWithOverride(model.Region)
if providerData.KMSCustomEndpoint != "" { | ||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.KMSCustomEndpoint)) | ||
} else { | ||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(providerData.GetRegion())) |
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.
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(providerData.GetRegion())) | |
apiClientConfigOptions = append(apiClientConfigOptions)) |
Since the KMS API already implemented the new multi-region concept, you don't need to set the region here (or better: the SDK should throw an error if you do 😄 ).
Apart from that, consider adding a unit tests for this func, see e.g. https://github.com/stackitcloud/terraform-provider-stackit/blob/8e776757ea2280d1222afe50c3024945b4d99eed/stackit/internal/services/git/utils/util_test.go
Description
relates to #1234
Checklist
make fmt
examples/
directory)make generate-docs
(will be checked by CI)make test
(will be checked by CI)make lint
(will be checked by CI)