diff --git a/internal/provider/registration_token.go b/internal/provider/registration_token.go index efb8814..3192f99 100644 --- a/internal/provider/registration_token.go +++ b/internal/provider/registration_token.go @@ -57,44 +57,44 @@ func (r *RegistrationTokenResource) Schema(ctx context.Context, req resource.Sch MarkdownDescription: "Example resource", Attributes: map[string]schema.Attribute{ - "space_id": schema.StringAttribute{ // TODO: add check that either space or org needs to be set - MarkdownDescription: "Example configurable attribute with default value", + "space_id": schema.StringAttribute{ + MarkdownDescription: "Mondoo Space Identifier to create the token in.", Required: true, }, "mrn": schema.StringAttribute{ Computed: true, - MarkdownDescription: "Example identifier", + MarkdownDescription: "The Mondoo Resource Name (MRN) of the created token.", PlanModifiers: []planmodifier.String{ stringplanmodifier.UseStateForUnknown(), }, }, "description": schema.StringAttribute{ - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "Description of the token.", Optional: true, PlanModifiers: []planmodifier.String{ stringplanmodifier.UseStateForUnknown(), }, }, "no_expiration": schema.BoolAttribute{ // TODO: add check that either no_expiration or expires_in needs to be set - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "If set to true, the token will not expire.", Optional: true, }, "expires_in": schema.StringAttribute{ - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "The duration after which the token will expire. Format: 1h, 1d, 1w, 1m, 1y", Optional: true, }, "revoked": schema.BoolAttribute{ - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "If set to true, the token is revoked.", Optional: true, Computed: true, }, "expires_at": schema.StringAttribute{ - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "The date and time when the token will expire.", Optional: true, Computed: true, }, "result": schema.StringAttribute{ - Description: "The generated random string.", + Description: "The generated token.", Computed: true, Sensitive: true, }, diff --git a/internal/provider/service_account_resource.go b/internal/provider/service_account_resource.go index 039e02d..97c7fe8 100644 --- a/internal/provider/service_account_resource.go +++ b/internal/provider/service_account_resource.go @@ -59,7 +59,7 @@ func (r *ServiceAccountResource) Schema(ctx context.Context, req resource.Schema Attributes: map[string]schema.Attribute{ "name": schema.StringAttribute{ - MarkdownDescription: "Example configurable attribute", + MarkdownDescription: "Name of the service account.", Optional: true, Computed: true, PlanModifiers: []planmodifier.String{ @@ -67,28 +67,31 @@ func (r *ServiceAccountResource) Schema(ctx context.Context, req resource.Schema }, }, "description": schema.StringAttribute{ - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "Description of the service account.", Optional: true, Computed: true, - Default: stringdefault.StaticString("example value when not configured"), + Default: stringdefault.StaticString("Created by Terraform"), + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, }, "mrn": schema.StringAttribute{ Computed: true, - MarkdownDescription: "Example identifier", + MarkdownDescription: "The Mondoo Resource Name (MRN) of the created service account.", PlanModifiers: []planmodifier.String{ stringplanmodifier.UseStateForUnknown(), }, }, "space_id": schema.StringAttribute{ // TODO: add check that either space or org needs to be set - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "Mondoo Space Identifier to create the service account in.", Optional: true, }, "org_id": schema.StringAttribute{ - MarkdownDescription: "Example configurable attribute with default value", + MarkdownDescription: "Mondoo Organization Identifier to create the service account in.", Optional: true, }, "roles": schema.ListAttribute{ - MarkdownDescription: "tbd", + MarkdownDescription: "Roles to assign to the service account.", ElementType: types.StringType, Optional: true, Computed: true, @@ -120,6 +123,16 @@ func (r *ServiceAccountResource) Configure(ctx context.Context, req resource.Con r.client = client } +func getScope(data ServiceAccountResourceModel) string { + scopeMrn := "" + if data.SpaceID.ValueString() != "" { + scopeMrn = spacePrefix + data.SpaceID.ValueString() + } else if data.OrgID.ValueString() != "" { + scopeMrn = orgPrefix + data.OrgID.ValueString() + } + return scopeMrn +} + func (r *ServiceAccountResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { var data ServiceAccountResourceModel @@ -154,12 +167,8 @@ func (r *ServiceAccountResource) Create(ctx context.Context, req resource.Create rolesInput = append(rolesInput, mondoov1.RoleInput{Mrn: mondoov1.String(role)}) } - scopeMrn := "" - if data.SpaceID.ValueString() != "" { - scopeMrn = spacePrefix + data.SpaceID.ValueString() - } else if data.OrgID.ValueString() != "" { - scopeMrn = orgPrefix + data.OrgID.ValueString() - } else { + scopeMrn := getScope(data) + if scopeMrn == "" { resp.Diagnostics.AddError( "Either space_id or org_id needs to be set", "Either space_id or org_id needs to be set", @@ -287,8 +296,9 @@ func (r *ServiceAccountResource) Update(ctx context.Context, req resource.Update } `graphql:"updateServiceAccount(input: $input)"` } updateInput := mondoov1.UpdateServiceAccountInput{ - Mrn: mondoov1.String(data.Mrn.ValueString()), - Name: mondoov1.NewStringPtr(mondoov1.String(data.Name.ValueString())), + Mrn: mondoov1.String(data.Mrn.ValueString()), + Name: mondoov1.NewStringPtr(mondoov1.String(data.Name.ValueString())), + Notes: mondoov1.NewStringPtr(mondoov1.String(data.Description.ValueString())), } tflog.Trace(ctx, "UpdateServiceAccountInput", map[string]interface{}{ "input": fmt.Sprintf("%+v", updateInput), @@ -313,6 +323,15 @@ func (r *ServiceAccountResource) Delete(ctx context.Context, req resource.Delete return } + scopeMrn := getScope(data) + if scopeMrn == "" { + resp.Diagnostics.AddError( + "Either space_id or org_id needs to be set", + "Either space_id or org_id needs to be set", + ) + return + } + // Do GraphQL request to API to delete the resource. var deleteMutation struct { DeleteServiceAccounts struct { @@ -320,7 +339,7 @@ func (r *ServiceAccountResource) Delete(ctx context.Context, req resource.Delete } `graphql:"deleteServiceAccounts(input: $input)"` } deleteInput := mondoov1.DeleteServiceAccountsInput{ - ScopeMrn: mondoov1.String(spacePrefix + data.SpaceID.ValueString()), + ScopeMrn: mondoov1.String(scopeMrn), Mrns: []mondoov1.String{mondoov1.String(data.Mrn.ValueString())}, } tflog.Trace(ctx, "UpdateServiceAccountInput", map[string]interface{}{ diff --git a/internal/provider/space_resource.go b/internal/provider/space_resource.go index 576d4d4..3a8f398 100644 --- a/internal/provider/space_resource.go +++ b/internal/provider/space_resource.go @@ -53,18 +53,18 @@ func (r *SpaceResource) Schema(ctx context.Context, req resource.SchemaRequest, Attributes: map[string]schema.Attribute{ "name": schema.StringAttribute{ - MarkdownDescription: "Space Name", + MarkdownDescription: "Name of the space.", Optional: true, }, "id": schema.StringAttribute{ - MarkdownDescription: "Space identifier", + MarkdownDescription: "Id of the space. Must be globally within the organization.", Computed: true, PlanModifiers: []planmodifier.String{ stringplanmodifier.UseStateForUnknown(), }, }, "org_id": schema.StringAttribute{ - MarkdownDescription: "Organization where the space is created", + MarkdownDescription: "Id of the organization.", Required: true, }, },