|
| 1 | +package dcs |
| 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/config" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 16 | +) |
| 17 | + |
| 18 | +var dcsLoginWebCliNonUpdatableParams = []string{"instance_id", "password"} |
| 19 | + |
| 20 | +// @API DCS POST /v2/{project_id}/instances/{instance_id}/webcli/auth |
| 21 | +func ResourceDcsLoginWebCli() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + CreateContext: resourceDcsLoginWebCliCreate, |
| 24 | + ReadContext: resourceDcsLoginWebCliRead, |
| 25 | + UpdateContext: resourceDcsLoginWebCliUpdate, |
| 26 | + DeleteContext: resourceDcsLoginWebCliDelete, |
| 27 | + |
| 28 | + CustomizeDiff: config.FlexibleForceNew(dcsLoginWebCliNonUpdatableParams), |
| 29 | + |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "region": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Optional: true, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "instance_id": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + }, |
| 40 | + "password": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Sensitive: true, |
| 44 | + }, |
| 45 | + "enable_force_new": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), |
| 49 | + Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func resourceDcsLoginWebCliCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 56 | + cfg := meta.(*config.Config) |
| 57 | + region := cfg.GetRegion(d) |
| 58 | + |
| 59 | + var ( |
| 60 | + httpUrl = "v2/{project_id}/instances/{instance_id}/webcli/auth" |
| 61 | + product = "dcs" |
| 62 | + ) |
| 63 | + |
| 64 | + client, err := cfg.NewServiceClient(product, region) |
| 65 | + if err != nil { |
| 66 | + return diag.Errorf("error creating DCS client: %s", err) |
| 67 | + } |
| 68 | + |
| 69 | + instanceID := d.Get("instance_id").(string) |
| 70 | + |
| 71 | + createPath := client.Endpoint + httpUrl |
| 72 | + createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) |
| 73 | + createPath = strings.ReplaceAll(createPath, "{instance_id}", instanceID) |
| 74 | + |
| 75 | + reqBody := make(map[string]interface{}) |
| 76 | + if v, ok := d.GetOk("password"); ok { |
| 77 | + reqBody["password"] = v.(string) |
| 78 | + } |
| 79 | + |
| 80 | + createOpt := golangsdk.RequestOpts{ |
| 81 | + JSONBody: reqBody, |
| 82 | + MoreHeaders: map[string]string{ |
| 83 | + "Content-Type": "application/json", |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + _, err = client.Request("POST", createPath, &createOpt) |
| 88 | + if err != nil { |
| 89 | + return diag.Errorf("error creating DCS login web cli resource: %s", err) |
| 90 | + } |
| 91 | + |
| 92 | + generateUUID, err := uuid.GenerateUUID() |
| 93 | + if err != nil { |
| 94 | + return diag.Errorf("unable to generate ID: %s", err) |
| 95 | + } |
| 96 | + |
| 97 | + d.SetId(generateUUID) |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func resourceDcsLoginWebCliRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func resourceDcsLoginWebCliUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func resourceDcsLoginWebCliDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 111 | + errorMsg := "Deleting DCS WebCli login resource is not supported. The resource is only removed from the state" |
| 112 | + return diag.Diagnostics{ |
| 113 | + diag.Diagnostic{ |
| 114 | + Severity: diag.Warning, |
| 115 | + Summary: errorMsg, |
| 116 | + }, |
| 117 | + } |
| 118 | +} |
0 commit comments