Skip to content

Commit

Permalink
🧹 update description of the resources
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Nov 11, 2023
1 parent 301d089 commit 87f0b17
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
18 changes: 9 additions & 9 deletions internal/provider/registration_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
51 changes: 35 additions & 16 deletions internal/provider/service_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,39 @@ 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{
stringplanmodifier.UseStateForUnknown(),
},
},
"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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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),
Expand All @@ -313,14 +323,23 @@ 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 {
Mrns []mondoov1.String
} `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{}{
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/space_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down

0 comments on commit 87f0b17

Please sign in to comment.