Skip to content

Commit

Permalink
Merge pull request #22 from zevisert/authorization-resource-required-…
Browse files Browse the repository at this point in the history
…fields

Authorization resource required fields
  • Loading branch information
thulasirajkomminar authored Apr 15, 2024
2 parents 96a4a07 + d007ad3 commit 53b7a0e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/data-sources/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Read-Only:
Read-Only:

- `id` (String) A resource ID. Identifies a specific resource.
- `name` (String) The name of the resource. Note: not all resource types have a name property.
- `name` (String) The name of the resource. **Note:** not all resource types have a name property.
- `org` (String) An organization name. The organization that owns the resource.
- `org_id` (String) An organization ID. Identifies the organization that owns the resource.
- `type` (String) A resource type. Identifies the API resource's type (or kind).
2 changes: 1 addition & 1 deletion docs/data-sources/authorizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Read-Only:
Read-Only:

- `id` (String) A resource ID. Identifies a specific resource.
- `name` (String) The name of the resource. Note: not all resource types have a name property.
- `name` (String) The name of the resource. **Note:** not all resource types have a name property.
- `org` (String) An organization name. The organization that owns the resource.
- `org_id` (String) An organization ID. Identifies the organization that owns the resource.
- `type` (String) A resource type. Identifies the API resource's type (or kind).
9 changes: 6 additions & 3 deletions docs/resources/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ Required:

Required:

- `id` (String) A resource ID. Identifies a specific resource.
- `org_id` (String) An organization ID. Identifies the organization that owns the resource.
- `type` (String) A resource type. Identifies the API resource's type (or kind).

Optional:

- `name` (String) The name of the resource. **Note:** not all resource types have a name property.
- `id` (String) A resource ID. Identifies a specific resource.
- `org` (String) An organization name. The organization that owns the resource.
- `org_id` (String) An organization ID. Identifies the organization that owns the resource.

Read-Only:

- `name` (String) The name of the resource. **Note:** not all resource types have a name property.
10 changes: 4 additions & 6 deletions examples/resources/authorization/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ resource "influxdb_authorization" "signals" {
permissions = [{
action = "read"
resource = {
id = data.influxdb_bucket.signals.id
org_id = data.influxdb_organization.iot.id
type = "buckets"
id = data.influxdb_bucket.signals.id
type = "buckets"
}
},
{
action = "write"
resource = {
id = data.influxdb_bucket.signals.id
org_id = data.influxdb_organization.iot.id
type = "buckets"
id = data.influxdb_bucket.signals.id
type = "buckets"
}
}]
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/authorization_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (d *AuthorizationDataSource) Schema(ctx context.Context, req datasource.Sch
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the resource. Note: not all resource types have a name property.",
Description: "The name of the resource. **Note:** not all resource types have a name property.",
},
"org": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -182,7 +182,7 @@ func (d *AuthorizationDataSource) Read(ctx context.Context, req datasource.ReadR
for _, permissionData := range *authorization.Permissions {
permissionState := AuthorizationPermissionModel{
Action: types.StringValue(string(permissionData.Action)),
Resource: AuthorizationPermissionrResourceModel{
Resource: AuthorizationPermissionResourceModel{
Id: types.StringPointerValue(permissionData.Resource.Id),
Name: types.StringPointerValue(permissionData.Resource.Name),
Org: types.StringPointerValue(permissionData.Resource.Org),
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/authorization_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ type AuthorizationModel struct {

// AuthorizationPermissionModel maps InfluxDB authorization permission schema data.
type AuthorizationPermissionModel struct {
Action types.String `tfsdk:"action"`
Resource AuthorizationPermissionrResourceModel `tfsdk:"resource"`
Action types.String `tfsdk:"action"`
Resource AuthorizationPermissionResourceModel `tfsdk:"resource"`
}

// AuthorizationPermissionrResourceModel maps InfluxDB authorization permission resource schema data.
type AuthorizationPermissionrResourceModel struct {
// AuthorizationPermissionResourceModel maps InfluxDB authorization permission resource schema data.
type AuthorizationPermissionResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Org types.String `tfsdk:"org"`
Expand Down
56 changes: 40 additions & 16 deletions internal/provider/authorization_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,12 @@ func (r *AuthorizationResource) Schema(ctx context.Context, req resource.SchemaR
Required: true,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
Optional: true,
Description: "A resource ID. Identifies a specific resource.",
},
"name": schema.StringAttribute{
Computed: true,
Optional: true,
Description: "The name of the resource. **Note:** not all resource types have a name property.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"org": schema.StringAttribute{
Computed: true,
Expand All @@ -146,12 +142,46 @@ func (r *AuthorizationResource) Schema(ctx context.Context, req resource.SchemaR
},
},
"org_id": schema.StringAttribute{
Required: true,
Computed: true,
Optional: true,
Description: "An organization ID. Identifies the organization that owns the resource.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"type": schema.StringAttribute{
Required: true,
Description: "A resource type. Identifies the API resource's type (or kind).",
Validators: []validator.String{
stringvalidator.OneOf([]string{
"authorizations",
"buckets",
"dashboards",
"orgs",
"tasks",
"telegrafs",
"users",
"variables",
"secrets",
"labels",
"views",
"documents",
"notificationRules",
"notificationEndpoints",
"checks",
"dbrp",
"annotations",
"sources",
"scrapers",
"notebooks",
"remotes",
"replications",
"instance",
"flows",
"functions",
"subscriptions",
}...),
},
},
},
},
Expand Down Expand Up @@ -217,9 +247,7 @@ func (r *AuthorizationResource) Create(ctx context.Context, req resource.CreateR
plan.CreatedAt = types.StringValue(apiResponse.CreatedAt.String())
plan.UpdatedAt = types.StringValue(apiResponse.UpdatedAt.String())
plan.Description = types.StringValue(*apiResponse.AuthorizationUpdateRequest.Description)

permissionsResult := getPermissions(*apiResponse.Permissions)
plan.Permissions = permissionsResult
plan.Permissions = getPermissions(*apiResponse.Permissions)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
Expand Down Expand Up @@ -277,9 +305,7 @@ func (r *AuthorizationResource) Read(ctx context.Context, req resource.ReadReque
state.UpdatedAt = types.StringValue(authorization.UpdatedAt.String())
state.Description = types.StringValue(*authorization.AuthorizationUpdateRequest.Description)
state.Status = types.StringValue(string(*authorization.Status))

permissionsResult := getPermissions(*authorization.Permissions)
state.Permissions = permissionsResult
state.Permissions = getPermissions(*authorization.Permissions)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down Expand Up @@ -325,9 +351,7 @@ func (r *AuthorizationResource) Update(ctx context.Context, req resource.UpdateR
plan.CreatedAt = types.StringValue(apiResponse.CreatedAt.String())
plan.UpdatedAt = types.StringValue(apiResponse.UpdatedAt.String())
plan.Description = types.StringValue(*apiResponse.AuthorizationUpdateRequest.Description)

permissionsResult := getPermissions(*apiResponse.Permissions)
plan.Permissions = permissionsResult
plan.Permissions = getPermissions(*apiResponse.Permissions)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
Expand Down Expand Up @@ -387,7 +411,7 @@ func getPermissions(permissions []domain.Permission) []AuthorizationPermissionMo
for _, permission := range permissions {
permissionState := AuthorizationPermissionModel{
Action: types.StringValue(string(permission.Action)),
Resource: AuthorizationPermissionrResourceModel{
Resource: AuthorizationPermissionResourceModel{
Id: types.StringPointerValue(permission.Resource.Id),
Name: types.StringPointerValue(permission.Resource.Name),
Type: types.StringValue(string(permission.Resource.Type)),
Expand Down
10 changes: 4 additions & 6 deletions internal/provider/authorization_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,15 @@ resource "influxdb_authorization" "test" {
permissions = [{
action = "read"
resource = {
id = influxdb_bucket.test.id
org_id = "`+os.Getenv("INFLUXDB_ORG_ID")+`"
type = "buckets"
id = influxdb_bucket.test.id
type = "buckets"
}
},
{
action = "write"
resource = {
id = influxdb_bucket.test.id
org_id = "`+os.Getenv("INFLUXDB_ORG_ID")+`"
type = "buckets"
id = influxdb_bucket.test.id
type = "buckets"
}
}]
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/authorizations_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (d *AuthorizationsDataSource) Schema(ctx context.Context, req datasource.Sc
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the resource. Note: not all resource types have a name property.",
Description: "The name of the resource. **Note:** not all resource types have a name property.",
},
"org": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -177,7 +177,7 @@ func (d *AuthorizationsDataSource) Read(ctx context.Context, req datasource.Read
for _, permissionData := range *authorization.Permissions {
permissionState := AuthorizationPermissionModel{
Action: types.StringValue(string(permissionData.Action)),
Resource: AuthorizationPermissionrResourceModel{
Resource: AuthorizationPermissionResourceModel{
Id: types.StringPointerValue(permissionData.Resource.Id),
Name: types.StringPointerValue(permissionData.Resource.Name),
Org: types.StringPointerValue(permissionData.Resource.Org),
Expand Down

0 comments on commit 53b7a0e

Please sign in to comment.