Skip to content

Commit

Permalink
feat: Add used_in_production arg in subaccount creation and update (#288
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Thanhphan1147 authored Jul 13, 2023
1 parent 00fed85 commit 6687594
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
14 changes: 7 additions & 7 deletions docs/resources/subaccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ resource "btp_subaccount" "my_project_on_azure" {
- `description` (String) A description of the subaccount for customer-facing UIs.
- `labels` (Map of Set of String) The set of words or phrases assigned to the subaccount.
- `parent_id` (String) The ID of the subaccount’s parent entity. If the subaccount is located directly in the global account (not in a directory), then this is the ID of the global account.
- `usage` (String) Shows whether the subaccount is used for production purposes. This flag can help your cloud operator to take appropriate action when handling incidents that are related to mission-critical accounts in production systems. Do not apply for subaccounts that are used for nonproduction purposes, such as development, testing, and demos. Applying this setting this does not modify the subaccount. Possible values are:

| value | description |
| --- | --- |
| `UNSET` | Global account or subaccount admin has not set the production-relevancy flag (default value). |
| `NOT_USED_FOR_PRODUCTION` | The subaccount is not used for production purposes. |
| `USED_FOR_PRODUCTION` | The subaccount is used for production purposes. |

### Read-Only

Expand Down Expand Up @@ -94,13 +101,6 @@ resource "btp_subaccount" "my_project_on_azure" {
| `MIGRATION_FAILED` | The migration of the subaccount failed and the subaccount was not migrated. |
| `ROLLBACK_MIGRATION_PROCESSING` | The migration of the subaccount was rolled back and the subaccount is not migrated. |
| `SUSPENSION_FAILED` | The suspension operations failed. |
- `usage` (String) Shows whether the subaccount is used for production purposes. This flag can help your cloud operator to take appropriate action when handling incidents that are related to mission-critical accounts in production systems. Do not apply for subaccounts that are used for nonproduction purposes, such as development, testing, and demos. Applying this setting this does not modify the subaccount. Possible values are:

| value | description |
| --- | --- |
| `UNSET` | Global account or subaccount admin has not set the production-relevancy flag (default value). |
| `NOT_USED_FOR_PRODUCTION` | The subaccount is not used for production purposes. |
| `USED_FOR_PRODUCTION` | The subaccount is used for production purposes. |

## Import

Expand Down
16 changes: 8 additions & 8 deletions internal/btpcli/facade_accounts_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ type SubaccountCreateInput struct { // TODO support all options
}

type SubaccountUpdateInput struct {
BetaEnabled bool `btpcli:"betaEnabled"`
Description string `btpcli:"description"`
Directory string `btpcli:"directoryID"`
DisplayName string `btpcli:"displayName"`
Labels map[string][]string `btpcli:"labels"`
SubaccountId string `btpcli:"subaccount"`
Globalaccount string `btpcli:"globalAccount"`
// UsedForProduction bool `btpcli:"usedForProduction"`
BetaEnabled bool `btpcli:"betaEnabled"`
Description string `btpcli:"description"`
Directory string `btpcli:"directoryID"`
DisplayName string `btpcli:"displayName"`
Labels map[string][]string `btpcli:"labels"`
SubaccountId string `btpcli:"subaccount"`
Globalaccount string `btpcli:"globalAccount"`
UsedForProduction bool `btpcli:"usedForProduction"`
}

func (f *accountsSubaccountFacade) Create(ctx context.Context, args *SubaccountCreateInput) (cis.SubaccountResponseObject, CommandResponse, error) {
Expand Down
11 changes: 6 additions & 5 deletions internal/btpcli/facade_accounts_subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ func TestAccountsSubaccountFacade_Update(t *testing.T) {
srvCalled = true

assertCall(t, r, command, ActionUpdate, map[string]string{
"subaccount": subaccountId,
"displayName": displayName,
"description": description,
"betaEnabled": "false",
"globalAccount": globalAccount,
"subaccount": subaccountId,
"displayName": displayName,
"description": description,
"betaEnabled": "false",
"globalAccount": globalAccount,
"usedForProduction": "false",
})

}))
Expand Down
24 changes: 24 additions & 0 deletions internal/provider/resource_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ __Further documentation:__
getFormattedValueAsTableRow("`NOT_USED_FOR_PRODUCTION`", "The subaccount is not used for production purposes.") +
getFormattedValueAsTableRow("`USED_FOR_PRODUCTION`", "The subaccount is used for production purposes."),
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf([]string{
"USED_FOR_PRODUCTION",
"NOT_USED_FOR_PRODUCTION",
"UNSET",
}...),
},
},
},
}
Expand Down Expand Up @@ -236,6 +244,11 @@ func (rs *subaccountResource) Create(ctx context.Context, req resource.CreateReq
args.Labels = labels
}

if !plan.Usage.IsUnknown() && !plan.Usage.IsNull() {
usedForProduction := plan.Usage.ValueString()
args.UsedForProduction = usedForProduction
}

cliRes, _, err := rs.cli.Accounts.Subaccount.Create(ctx, &args)

if err != nil {
Expand Down Expand Up @@ -299,6 +312,17 @@ func (rs *subaccountResource) Update(ctx context.Context, req resource.UpdateReq
plan.Labels.ElementsAs(ctx, &labels, false)
args.Labels = labels

// Specifically in BTP CLI's update subcommand, usage is specified as a boolean
// As shown in the cli's documentation
// --used-for-production [BOOL]
// So we modify the input arg here
if !plan.Usage.IsUnknown() && !plan.Usage.IsNull() {
if !plan.Usage.Equal(types.StringValue("UNSET")) {
usedForProduction := plan.Usage.ValueString()
args.UsedForProduction = (usedForProduction == "USED_FOR_PRODUCTION")
}
}

cliRes, _, err := rs.cli.Accounts.Subaccount.Update(ctx, &args)
if err != nil {
resp.Diagnostics.AddError("API Error Updating Resource Subaccount", fmt.Sprintf("%s", err))
Expand Down

0 comments on commit 6687594

Please sign in to comment.