Skip to content

Commit

Permalink
add credentials_group_id to data credential (#176)
Browse files Browse the repository at this point in the history
* Add `credentials_group_id` attribute to `stackit_object_storage_credential` data source
* Automated docs update


---------

Co-authored-by: Dean Oren <deangili.oren@mail.schwarz>
Co-authored-by: do87 <do87@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 7, 2023
1 parent cffdf41 commit 6e750dc
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 16 deletions.
9 changes: 9 additions & 0 deletions docs/data-sources/object_storage_credential.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ resource "stackit_object_storage_credential" "example" {
object_storage_project_id = stackit_object_storage_project.example.id
}
data "stackit_object_storage_credentials_group" "example" {
depends_on = [stackit_object_storage_credential.example]
object_storage_project_id = stackit_object_storage_project.example.id
name = "default"
}
data "stackit_object_storage_credential" "ex1" {
object_storage_project_id = stackit_object_storage_project.example.id
credentials_group_id = data.stackit_object_storage_credentials_group.example.id
id = stackit_object_storage_credential.example.id
}
data "stackit_object_storage_credential" "ex2" {
object_storage_project_id = stackit_object_storage_project.example.id
credentials_group_id = data.stackit_object_storage_credentials_group.example.id
display_name = stackit_object_storage_credential.example.display_name
}
```
Expand All @@ -43,6 +51,7 @@ data "stackit_object_storage_credential" "ex2" {

### Required

- `credentials_group_id` (String) the credentials group ID
- `object_storage_project_id` (String) The ID returned from `stackit_object_storage_project`

### Optional
Expand Down
7 changes: 5 additions & 2 deletions docs/data-sources/object_storage_credentials_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ data "stackit_object_storage_credentials_group" "example" {

### Required

- `id` (String) the credential group ID
- `object_storage_project_id` (String) The ID returned from `stackit_object_storage_project`

### Read-Only
### Optional

- `id` (String) the credential group ID
- `name` (String) the credential group's display name

### Read-Only

- `urn` (String) credential group URN


Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ resource "stackit_object_storage_credential" "example" {
object_storage_project_id = stackit_object_storage_project.example.id
}

data "stackit_object_storage_credentials_group" "example" {
depends_on = [stackit_object_storage_credential.example]
object_storage_project_id = stackit_object_storage_project.example.id
name = "default"
}

data "stackit_object_storage_credential" "ex1" {
object_storage_project_id = stackit_object_storage_project.example.id
credentials_group_id = data.stackit_object_storage_credentials_group.example.id
id = stackit_object_storage_credential.example.id
}

data "stackit_object_storage_credential" "ex2" {
object_storage_project_id = stackit_object_storage_project.example.id
credentials_group_id = data.stackit_object_storage_credentials_group.example.id
display_name = stackit_object_storage_credential.example.display_name
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func (r DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *
return
}

params := &accesskey.GetParams{}
credGroup := config.CredentialsGroupID.ValueString()
params := &accesskey.GetParams{
CredentialsGroup: &credGroup,
}
res, err := c.ObjectStorage.AccessKey.Get(ctx, config.ObjectStorageProjectID.ValueString(), params)
if agg := common.Validate(&resp.Diagnostics, res, err, "JSON200.AccessKeys"); agg != nil {
resp.Diagnostics.AddError("failed to list credentials", agg.Error())
Expand All @@ -45,11 +48,9 @@ func (r DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *
config.Expiry = types.StringValue(k.Expires)
diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

break
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const run_this_test = false
const run_this_test = true

func TestAcc_ObjectStorageCredential(t *testing.T) {
if !common.ShouldAccTestRun(run_this_test) {
Expand Down Expand Up @@ -50,14 +50,22 @@ func config() string {
object_storage_project_id = stackit_object_storage_project.example.id
}
data "stackit_object_storage_credential" "ex1" {
data "stackit_object_storage_credentials_group" "example" {
depends_on = [stackit_object_storage_credential.example]
object_storage_project_id = stackit_object_storage_project.example.id
id = stackit_object_storage_credential.example.id
name = "default"
}
data "stackit_object_storage_credential" "ex1" {
object_storage_project_id = stackit_object_storage_project.example.id
credentials_group_id = data.stackit_object_storage_credentials_group.example.id
id = stackit_object_storage_credential.example.id
}
data "stackit_object_storage_credential" "ex2" {
object_storage_project_id = stackit_object_storage_project.example.id
display_name = stackit_object_storage_credential.example.display_name
object_storage_project_id = stackit_object_storage_project.example.id
credentials_group_id = data.stackit_object_storage_credentials_group.example.id
display_name = stackit_object_storage_credential.example.display_name
}
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type Credential struct {
ID types.String `tfsdk:"id"`
ObjectStorageProjectID types.String `tfsdk:"object_storage_project_id"`
CredentialsGroupID types.String `tfsdk:"credentials_group_id"`
Expiry types.String `tfsdk:"expiry"`
DisplayName types.String `tfsdk:"display_name"`
}
Expand All @@ -41,6 +42,11 @@ func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, r
},
},

"credentials_group_id": schema.StringAttribute{
Description: "the credentials group ID",
Required: true,
},

"expiry": schema.StringAttribute{
Computed: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func (r DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *
return
}

if data.ID.ValueString() == "" && data.Name.ValueString() == "" {
resp.Diagnostics.AddError("missing configuration", "either name or id must be provided")
return
}

res, err := c.ObjectStorage.CredentialsGroup.Get(ctx, data.ObjectStorageProjectID.ValueString())
if agg := common.Validate(&resp.Diagnostics, res, err, "JSON200.CredentialsGroups"); agg != nil {
resp.Diagnostics.AddError("failed to read credential groups", agg.Error())
Expand All @@ -27,12 +32,14 @@ func (r DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *

found := false
for _, group := range res.JSON200.CredentialsGroups {
if group.CredentialsGroupID == data.ID.ValueString() {
found = true
data.Name = types.StringValue(group.DisplayName)
data.URN = types.StringValue(group.URN)
break
if group.CredentialsGroupID != data.ID.ValueString() && group.DisplayName != data.Name.ValueString() {
continue
}
found = true
data.ID = types.StringValue(group.CredentialsGroupID)
data.Name = types.StringValue(group.DisplayName)
data.URN = types.StringValue(group.URN)
break
}

if !found {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, r
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "the credential group ID",
Required: true,
Optional: true,
Computed: true,
},

"object_storage_project_id": schema.StringAttribute{
Expand All @@ -33,6 +34,7 @@ func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, r

"name": schema.StringAttribute{
Description: "the credential group's display name",
Optional: true,
Computed: true,
},

Expand Down

0 comments on commit 6e750dc

Please sign in to comment.