|
| 1 | +// Generated by PMS #672 |
| 2 | +package sms |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-multierror" |
| 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/tidwall/gjson" |
| 12 | + |
| 13 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" |
| 16 | +) |
| 17 | + |
| 18 | +func DataSourceSmsSourceServerOverview() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + ReadContext: dataSourceSmsSourceServerOverviewRead, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "waiting": { |
| 24 | + Type: schema.TypeInt, |
| 25 | + Computed: true, |
| 26 | + Description: `Indicates the number of servers that are in a waiting migration status.`, |
| 27 | + }, |
| 28 | + "replicate": { |
| 29 | + Type: schema.TypeInt, |
| 30 | + Computed: true, |
| 31 | + Description: `Indicates the number of servers that are in a replicating migration status.`, |
| 32 | + }, |
| 33 | + "syncing": { |
| 34 | + Type: schema.TypeInt, |
| 35 | + Computed: true, |
| 36 | + Description: `Indicates the number of servers that are in a synchronizing migration status.`, |
| 37 | + }, |
| 38 | + "stopped": { |
| 39 | + Type: schema.TypeInt, |
| 40 | + Computed: true, |
| 41 | + Description: `Indicates the number of servers that are in a paused migration status.`, |
| 42 | + }, |
| 43 | + "deleting": { |
| 44 | + Type: schema.TypeInt, |
| 45 | + Computed: true, |
| 46 | + Description: `Indicates the number of servers that are in a deleting migration status.`, |
| 47 | + }, |
| 48 | + "cutovering": { |
| 49 | + Type: schema.TypeInt, |
| 50 | + Computed: true, |
| 51 | + Description: `Indicates the number of servers whose paired target servers are being launched.`, |
| 52 | + }, |
| 53 | + "unavailable": { |
| 54 | + Type: schema.TypeInt, |
| 55 | + Computed: true, |
| 56 | + Description: `Indicates the number of servers that fail the environment check.`, |
| 57 | + }, |
| 58 | + "stopping": { |
| 59 | + Type: schema.TypeInt, |
| 60 | + Computed: true, |
| 61 | + Description: `Indicates the number of servers that are in a stopping migration status.`, |
| 62 | + }, |
| 63 | + "skipping": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: `Indicates the number of servers that are in a skipping migration status.`, |
| 67 | + }, |
| 68 | + "finished": { |
| 69 | + Type: schema.TypeInt, |
| 70 | + Computed: true, |
| 71 | + Description: `Indicates the number of servers whose paired target servers have been launched.`, |
| 72 | + }, |
| 73 | + "initialize": { |
| 74 | + Type: schema.TypeInt, |
| 75 | + Computed: true, |
| 76 | + Description: `Indicates the number of servers that are in an initializing migration status.`, |
| 77 | + }, |
| 78 | + "error": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + Description: `Indicates the number of servers that are in an error migration status.`, |
| 82 | + }, |
| 83 | + "cloning": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Computed: true, |
| 86 | + Description: `Indicates the number of servers whose paired target servers are being cloned.`, |
| 87 | + }, |
| 88 | + "unconfigured": { |
| 89 | + Type: schema.TypeInt, |
| 90 | + Computed: true, |
| 91 | + Description: `Indicates the number of servers that do not have target server configurations.`, |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +type SourceServerOverviewDSWrapper struct { |
| 98 | + *schemas.ResourceDataWrapper |
| 99 | + Config *config.Config |
| 100 | +} |
| 101 | + |
| 102 | +func newSourceServerOverviewDSWrapper(d *schema.ResourceData, meta interface{}) *SourceServerOverviewDSWrapper { |
| 103 | + return &SourceServerOverviewDSWrapper{ |
| 104 | + ResourceDataWrapper: schemas.NewSchemaWrapper(d), |
| 105 | + Config: meta.(*config.Config), |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func dataSourceSmsSourceServerOverviewRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 110 | + wrapper := newSourceServerOverviewDSWrapper(d, meta) |
| 111 | + showOverviewRst, err := wrapper.ShowOverview() |
| 112 | + if err != nil { |
| 113 | + return diag.FromErr(err) |
| 114 | + } |
| 115 | + |
| 116 | + id, err := uuid.GenerateUUID() |
| 117 | + if err != nil { |
| 118 | + return diag.FromErr(err) |
| 119 | + } |
| 120 | + d.SetId(id) |
| 121 | + |
| 122 | + err = wrapper.showOverviewToSchema(showOverviewRst) |
| 123 | + if err != nil { |
| 124 | + return diag.FromErr(err) |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +// @API SMS GET /v3/sources/overview |
| 131 | +func (w *SourceServerOverviewDSWrapper) ShowOverview() (*gjson.Result, error) { |
| 132 | + client, err := w.NewClient(w.Config, "sms") |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + uri := "/v3/sources/overview" |
| 138 | + return httphelper.New(client). |
| 139 | + Method("GET"). |
| 140 | + URI(uri). |
| 141 | + Request(). |
| 142 | + Result() |
| 143 | +} |
| 144 | + |
| 145 | +func (w *SourceServerOverviewDSWrapper) showOverviewToSchema(body *gjson.Result) error { |
| 146 | + d := w.ResourceData |
| 147 | + mErr := multierror.Append(nil, |
| 148 | + d.Set("waiting", body.Get("waiting").Value()), |
| 149 | + d.Set("replicate", body.Get("replicate").Value()), |
| 150 | + d.Set("syncing", body.Get("syncing").Value()), |
| 151 | + d.Set("stopped", body.Get("stopped").Value()), |
| 152 | + d.Set("deleting", body.Get("deleting").Value()), |
| 153 | + d.Set("cutovering", body.Get("cutovering").Value()), |
| 154 | + d.Set("unavailable", body.Get("unavailable").Value()), |
| 155 | + d.Set("stopping", body.Get("stopping").Value()), |
| 156 | + d.Set("skipping", body.Get("skipping").Value()), |
| 157 | + d.Set("finished", body.Get("finished").Value()), |
| 158 | + d.Set("initialize", body.Get("initialize").Value()), |
| 159 | + d.Set("error", body.Get("error").Value()), |
| 160 | + d.Set("cloning", body.Get("cloning").Value()), |
| 161 | + d.Set("unconfigured", body.Get("unconfigured").Value()), |
| 162 | + ) |
| 163 | + return mErr.ErrorOrNil() |
| 164 | +} |
0 commit comments