|
| 1 | +package hss |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/go-uuid" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | + |
| 13 | + "github.com/chnsz/golangsdk" |
| 14 | + |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 17 | +) |
| 18 | + |
| 19 | +var containerNetworkClusterSyncNonUpdatableParams = []string{ |
| 20 | + "enterprise_project_id", |
| 21 | +} |
| 22 | + |
| 23 | +// @API HSS GET /v5/{project_id}/container-network/cluster/sync |
| 24 | +func ResourceContainerNetworkClusterSync() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + CreateContext: resourceContainerNetworkClusterSyncCreate, |
| 27 | + ReadContext: resourceContainerNetworkClusterSyncRead, |
| 28 | + UpdateContext: resourceContainerNetworkClusterSyncUpdate, |
| 29 | + DeleteContext: resourceContainerNetworkClusterSyncDelete, |
| 30 | + |
| 31 | + CustomizeDiff: config.FlexibleForceNew(containerNetworkClusterSyncNonUpdatableParams), |
| 32 | + |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "region": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Computed: true, |
| 38 | + ForceNew: true, |
| 39 | + }, |
| 40 | + "enterprise_project_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + }, |
| 44 | + "enable_force_new": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), |
| 48 | + Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func buildContainerNetworkClusterSyncQueryParams(epsId string) string { |
| 55 | + if epsId != "" { |
| 56 | + return fmt.Sprintf("?enterprise_project_id=%v", epsId) |
| 57 | + } |
| 58 | + |
| 59 | + return "" |
| 60 | +} |
| 61 | + |
| 62 | +func resourceContainerNetworkClusterSyncCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 63 | + var ( |
| 64 | + cfg = meta.(*config.Config) |
| 65 | + region = cfg.GetRegion(d) |
| 66 | + product = "hss" |
| 67 | + epsId = cfg.GetEnterpriseProjectID(d) |
| 68 | + ) |
| 69 | + |
| 70 | + client, err := cfg.NewServiceClient(product, region) |
| 71 | + if err != nil { |
| 72 | + return diag.Errorf("error creating HSS client: %s", err) |
| 73 | + } |
| 74 | + |
| 75 | + requestPath := client.Endpoint + "v5/{project_id}/container-network/cluster/sync" |
| 76 | + requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID) |
| 77 | + requestPath += buildContainerNetworkClusterSyncQueryParams(epsId) |
| 78 | + requestOpt := golangsdk.RequestOpts{ |
| 79 | + KeepResponseBody: true, |
| 80 | + } |
| 81 | + |
| 82 | + _, err = client.Request("GET", requestPath, &requestOpt) |
| 83 | + if err != nil { |
| 84 | + return diag.Errorf("error syncing HSS container network cluster: %s", err) |
| 85 | + } |
| 86 | + |
| 87 | + generateUUID, err := uuid.GenerateUUID() |
| 88 | + if err != nil { |
| 89 | + return diag.Errorf("unable to generate ID: %s", err) |
| 90 | + } |
| 91 | + |
| 92 | + d.SetId(generateUUID) |
| 93 | + |
| 94 | + return resourceContainerNetworkClusterSyncRead(ctx, d, meta) |
| 95 | +} |
| 96 | + |
| 97 | +func resourceContainerNetworkClusterSyncRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 98 | + // No processing is performed in the 'Read()' method because the resource is a one-time action resource. |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func resourceContainerNetworkClusterSyncUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 103 | + // No processing is performed in the 'Update()' method because the resource is a one-time action resource. |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func resourceContainerNetworkClusterSyncDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 108 | + errorMsg := `This resource is a one-time action resource used to sync HSS container network cluster. Deleting this |
| 109 | + resource will not clear the corresponding request record, but will only remove the resource information from the |
| 110 | + tf state file.` |
| 111 | + return diag.Diagnostics{ |
| 112 | + diag.Diagnostic{ |
| 113 | + Severity: diag.Warning, |
| 114 | + Summary: errorMsg, |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
0 commit comments