Onboard object storage credentials group (#74)

* Onboard credentials group resource

* Update object storage acc test

* Fix typo

* Generate docs, add examples, fix typos

* Refactor map fiels

* Add unit test for readCredentialsGroups

* Fix lint issues§
g

* Use projectId from Model

* Fix errors§
g

* Fix test

* Simplify signature of enableProject

* Change comment

* Add TestEnableProject

* Rename variable

* Add enableProject test

* Remove unused test setting

* Fix wrong error message

* Improve test case, rename mocked data

* Fix typo

* Removed unnecessary test case

* Removed unnecessary test case

---------

Co-authored-by: Henrique Santos <henrique.santos@freiheit.com>
This commit is contained in:
Vicente Pinto 2023-10-12 09:34:38 +01:00 committed by GitHub
parent fee47a6400
commit b8d2d94156
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 1011 additions and 14 deletions

View file

@ -155,8 +155,15 @@ func (r *bucketResource) Create(ctx context.Context, req resource.CreateRequest,
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "bucket_name", bucketName)
// Create new recordset
_, err := r.client.CreateBucket(ctx, projectId, bucketName).Execute()
// Handle project init
err := enableProject(ctx, &model, r.client)
if resp.Diagnostics.HasError() {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating bucket", fmt.Sprintf("Enabling object storage project before creation: %v", err))
return
}
// Create new bucket
_, err = r.client.CreateBucket(ctx, projectId, bucketName).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating bucket", fmt.Sprintf("Calling API: %v", err))
return
@ -294,3 +301,19 @@ func mapFields(bucketResp *objectstorage.GetBucketResponse, model *Model) error
model.URLVirtualHostedStyle = types.StringPointerValue(bucket.UrlVirtualHostedStyle)
return nil
}
type objectStorageClient interface {
CreateProjectExecute(ctx context.Context, projectId string) (*objectstorage.GetProjectResponse, error)
}
// enableProject enables object storage for the specified project. If the project is already enabled, nothing happens
func enableProject(ctx context.Context, model *Model, client objectStorageClient) error {
projectId := model.ProjectId.ValueString()
// From the object storage OAS: Creation will also be successful if the project is already enabled, but will not create a duplicate
_, err := client.CreateProjectExecute(ctx, projectId)
if err != nil {
return fmt.Errorf("failed to create object storage project: %w", err)
}
return nil
}