|
| 1 | +package workspace |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-uuid" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 11 | + |
| 12 | + "github.com/chnsz/golangsdk" |
| 13 | + |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 17 | +) |
| 18 | + |
| 19 | +var appActionNonUpdatableParams = []string{ |
| 20 | + "app_restrict_rule_switch", |
| 21 | +} |
| 22 | + |
| 23 | +// @API Workspace PATCH /v1/{project_id}/app-center/profiles |
| 24 | +// @API Workspace GET /v1/{project_id}/app-center/profiles |
| 25 | +func ResourceAppAction() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + CreateContext: resourceAppActionCreate, |
| 28 | + ReadContext: resourceAppActionRead, |
| 29 | + UpdateContext: resourceAppActionUpdate, |
| 30 | + DeleteContext: resourceAppActionDelete, |
| 31 | + |
| 32 | + CustomizeDiff: config.FlexibleForceNew(appActionNonUpdatableParams), |
| 33 | + |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "region": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Computed: true, |
| 39 | + ForceNew: true, |
| 40 | + Description: `The region where the tenant profiles are located.`, |
| 41 | + }, |
| 42 | + "app_restrict_rule_switch": { |
| 43 | + Type: schema.TypeBool, |
| 44 | + Required: true, |
| 45 | + Description: `Whether to enable the application restriction rule switch.`, |
| 46 | + }, |
| 47 | + |
| 48 | + // Internal parameters. |
| 49 | + "enable_force_new": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), |
| 53 | + Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func buildAppActionBodyParams(d *schema.ResourceData) map[string]interface{} { |
| 60 | + body := map[string]interface{}{ |
| 61 | + "app_restrict_rule_switch": d.Get("app_restrict_rule_switch"), |
| 62 | + } |
| 63 | + return body |
| 64 | +} |
| 65 | + |
| 66 | +func resourceAppActionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 67 | + var ( |
| 68 | + cfg = meta.(*config.Config) |
| 69 | + region = cfg.GetRegion(d) |
| 70 | + httpUrl = "v1/{project_id}/app-center/profiles" |
| 71 | + ) |
| 72 | + |
| 73 | + client, err := cfg.NewServiceClient("workspace", region) |
| 74 | + if err != nil { |
| 75 | + return diag.Errorf("error creating Workspace client: %s", err) |
| 76 | + } |
| 77 | + |
| 78 | + createPath := client.Endpoint + httpUrl |
| 79 | + createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) |
| 80 | + |
| 81 | + opt := golangsdk.RequestOpts{ |
| 82 | + KeepResponseBody: true, |
| 83 | + MoreHeaders: map[string]string{ |
| 84 | + "Content-Type": "application/json", |
| 85 | + }, |
| 86 | + JSONBody: buildAppActionBodyParams(d), |
| 87 | + } |
| 88 | + |
| 89 | + _, err = client.Request("PATCH", createPath, &opt) |
| 90 | + if err != nil { |
| 91 | + return diag.Errorf("error creating app action: %s", err) |
| 92 | + } |
| 93 | + |
| 94 | + randomUUID, err := uuid.GenerateUUID() |
| 95 | + if err != nil { |
| 96 | + return diag.Errorf("unable to generate ID: %s", err) |
| 97 | + } |
| 98 | + d.SetId(randomUUID) |
| 99 | + |
| 100 | + return resourceAppActionRead(ctx, d, meta) |
| 101 | +} |
| 102 | + |
| 103 | +func resourceAppActionRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 104 | + var ( |
| 105 | + cfg = meta.(*config.Config) |
| 106 | + region = cfg.GetRegion(d) |
| 107 | + httpUrl = "v1/{project_id}/app-center/profiles" |
| 108 | + ) |
| 109 | + |
| 110 | + client, err := cfg.NewServiceClient("workspace", region) |
| 111 | + if err != nil { |
| 112 | + return diag.Errorf("error creating Workspace client: %s", err) |
| 113 | + } |
| 114 | + |
| 115 | + getPath := client.Endpoint + httpUrl |
| 116 | + getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID) |
| 117 | + |
| 118 | + opt := golangsdk.RequestOpts{ |
| 119 | + KeepResponseBody: true, |
| 120 | + MoreHeaders: map[string]string{ |
| 121 | + "Content-Type": "application/json", |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + response, err := client.Request("GET", getPath, &opt) |
| 126 | + if err != nil { |
| 127 | + return common.CheckDeletedDiag(d, err, "error retrieving app action") |
| 128 | + } |
| 129 | + |
| 130 | + respBody, err := utils.FlattenResponse(response) |
| 131 | + if err != nil { |
| 132 | + return diag.FromErr(err) |
| 133 | + } |
| 134 | + |
| 135 | + appRestrictRuleSwitch := utils.PathSearch("app_restrict_rule_switch", respBody, false) |
| 136 | + mErr := d.Set("app_restrict_rule_switch", appRestrictRuleSwitch.(bool)) |
| 137 | + if mErr != nil { |
| 138 | + return diag.Errorf("error setting app_restrict_rule_switch: %s", mErr) |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +func resourceAppActionUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func resourceAppActionDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 149 | + errorMsg := `This resource is used to manage the tenant profile settings. Deleting this resource will not |
| 150 | + clear the corresponding configuration, but will only remove the resource information from the tfstate file.` |
| 151 | + return diag.Diagnostics{ |
| 152 | + diag.Diagnostic{ |
| 153 | + Severity: diag.Warning, |
| 154 | + Summary: errorMsg, |
| 155 | + }, |
| 156 | + } |
| 157 | +} |
0 commit comments