Skip to content

Commit

Permalink
APP-15932 - Add ID to resource permission and readme note
Browse files Browse the repository at this point in the history
  • Loading branch information
jzolo22 committed Nov 7, 2024
1 parent c4b0c9a commit f829c77
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ This function is used to create the actual resource. It parses out the J1EntityM

Follow this general structure for your create function and look at other resource.go files to get an idea of what you may need here.

Note that if the resource you are creating does not return an `id` from the API, you will need to assign a unique id to the `data.Id` field. This is used by terraform to track the resource. The `user_group_membership` resource is one where this is necessary.

```go
func (r *J1EntityResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data J1EntityModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ResourcePermissionResource struct {
}

type ResourcePermissionModel struct {
ID types.String `tfsdk:"id"`
SubjectType types.String `json:"subjectType" tfsdk:"subject_type"`
SubjectId types.String `json:"subjectId" tfsdk:"subject_id"`
ResourceArea types.String `json:"resourceArea" tfsdk:"resource_area"`
Expand Down Expand Up @@ -63,6 +64,9 @@ func (*ResourcePermissionResource) Schema(ctx context.Context, req resource.Sche
resp.Schema = schema.Schema{
Description: "JupiterOne Resource Based Permission",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"subject_type": schema.StringAttribute{
Required: true,
Description: "The type of the subject that the resource permissions will be applied to (e.g. group).",
Expand Down Expand Up @@ -142,6 +146,8 @@ func (r *ResourcePermissionResource) Create(ctx context.Context, req resource.Cr
return
}

data.ID = types.StringValue(fmt.Sprintf("%s-%s-%s-%s-%s", data.SubjectType.ValueString(), data.SubjectId.ValueString(), data.ResourceArea.ValueString(), data.ResourceType.ValueString(), data.ResourceId.ValueString()))

tflog.Trace(ctx, "Set resource permission",
map[string]interface{}{"resourceArea": created.SetResourcePermission.ResourceArea})

Expand Down Expand Up @@ -176,6 +182,8 @@ func (r *ResourcePermissionResource) Update(ctx context.Context, req resource.Up
tflog.Trace(ctx, "Set resource permission",
map[string]interface{}{"resourceArea": created.SetResourcePermission.ResourceArea})

data.ID = types.StringValue(fmt.Sprintf("%s-%s-%s-%s-%s", data.SubjectType.ValueString(), data.SubjectId.ValueString(), data.ResourceArea.ValueString(), data.ResourceType.ValueString(), data.ResourceId.ValueString()))

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down

0 comments on commit f829c77

Please sign in to comment.