|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/url" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/ovh/go-ovh/ovh" |
| 12 | + "github.com/ovh/terraform-provider-ovh/ovh/helpers" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceCloudProjectInstanceActiveMonthlyBilling() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceCloudProjectInstanceActiveMonthlyBillingCreate, |
| 18 | + Update: resourceCloudProjectInstanceActiveMonthlyBillingUpdate, |
| 19 | + Read: resourceCloudProjectInstanceActiveMonthlyBillingRead, |
| 20 | + Delete: resourceCloudProjectInstanceActiveMonthlyBillingDelete, |
| 21 | + |
| 22 | + Timeouts: &schema.ResourceTimeout{ |
| 23 | + Create: schema.DefaultTimeout(45 * time.Minute), |
| 24 | + }, |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "service_name": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + Description: "The internal name of your dedicated server", |
| 32 | + }, |
| 33 | + "instance_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + Description: "Public Cloud instance ID", |
| 38 | + }, |
| 39 | + "wait_activation": { |
| 40 | + Type: schema.TypeBool, |
| 41 | + Optional: true, |
| 42 | + Default: false, |
| 43 | + Description: "Wait for monthly billing activation", |
| 44 | + }, |
| 45 | + |
| 46 | + // Computed |
| 47 | + "monthly_billing_since": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Monthly billing activated since", |
| 51 | + }, |
| 52 | + |
| 53 | + "monthly_billing_status": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Monthly billing status", |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func waitForCloudProjectInstanceActiveMonthlyBillingDone(client *ovh.Client, serviceName string, instanceId string) resource.StateRefreshFunc { |
| 63 | + return func() (interface{}, string, error) { |
| 64 | + r := &CloudProjectInstanceActiveMonthlyBillingResponse{} |
| 65 | + endpoint := fmt.Sprintf("/cloud/project/%s/instance/%s", url.PathEscape(serviceName), url.PathEscape(instanceId)) |
| 66 | + if err := client.Get(endpoint, r); err != nil { |
| 67 | + return r, "", err |
| 68 | + } |
| 69 | + |
| 70 | + log.Printf("[DEBUG] Pending active monthly billing: %s", r) |
| 71 | + return r, r.MonthlyBilling.Status, nil |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func resourceCloudProjectInstanceActiveMonthlyBillingCreate(d *schema.ResourceData, meta interface{}) error { |
| 76 | + config := meta.(*Config) |
| 77 | + serviceName := d.Get("service_name").(string) |
| 78 | + instanceId := d.Get("instance_id").(string) |
| 79 | + waitActivation := d.Get("wait_activation").(bool) |
| 80 | + |
| 81 | + endpoint := fmt.Sprintf( |
| 82 | + "/cloud/project/%s/instance/%s/activeMonthlyBilling", url.PathEscape(serviceName), url.PathEscape(instanceId), |
| 83 | + ) |
| 84 | + |
| 85 | + params := (&CloudProjectInstanceActiveMonthlyBillingCreateOpts{}).FromResource(d) |
| 86 | + |
| 87 | + r := &CloudProjectInstanceActiveMonthlyBillingResponse{} |
| 88 | + |
| 89 | + log.Printf("[DEBUG] Will install active monthly billing: %s", params) |
| 90 | + |
| 91 | + if err := config.OVHClient.Post(endpoint, params, r); err != nil { |
| 92 | + return fmt.Errorf("Error calling POST %s:\n\t %q", endpoint, err) |
| 93 | + } |
| 94 | + |
| 95 | + if waitActivation { |
| 96 | + log.Printf("[DEBUG] Waiting for active monthly billing %s:", r) |
| 97 | + |
| 98 | + stateConf := &resource.StateChangeConf{ |
| 99 | + Pending: []string{"activationPending"}, |
| 100 | + Target: []string{"ok"}, |
| 101 | + Refresh: waitForCloudProjectInstanceActiveMonthlyBillingDone(config.OVHClient, serviceName, instanceId), |
| 102 | + Timeout: 45 * time.Minute, |
| 103 | + Delay: 10 * time.Second, |
| 104 | + MinTimeout: 3 * time.Second, |
| 105 | + } |
| 106 | + |
| 107 | + if _, err := stateConf.WaitForState(); err != nil { |
| 108 | + return fmt.Errorf("waiting for active monthly billing (%s): %s", params, err) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + log.Printf("[DEBUG] Created active monthly billing %s", r) |
| 113 | + |
| 114 | + return CloudProjectInstanceActiveMonthlyBillingRead(d, meta) |
| 115 | +} |
| 116 | + |
| 117 | +func CloudProjectInstanceActiveMonthlyBillingRead(d *schema.ResourceData, meta interface{}) error { |
| 118 | + config := meta.(*Config) |
| 119 | + serviceName := d.Get("service_name").(string) |
| 120 | + instanceId := d.Get("instance_id").(string) |
| 121 | + |
| 122 | + r := &CloudProjectInstanceActiveMonthlyBillingResponse{} |
| 123 | + |
| 124 | + log.Printf("[DEBUG] Will read active monthly billing: %s", serviceName) |
| 125 | + |
| 126 | + endpoint := fmt.Sprintf("/cloud/project/%s/instance/%s", url.PathEscape(serviceName), url.PathEscape(instanceId)) |
| 127 | + |
| 128 | + if err := config.OVHClient.Get(endpoint, r); err != nil { |
| 129 | + return helpers.CheckDeleted(d, err, endpoint) |
| 130 | + } |
| 131 | + |
| 132 | + d.Set("monthly_billing_since", r.MonthlyBilling.Since) |
| 133 | + d.Set("monthly_billing_status", r.MonthlyBilling.Status) |
| 134 | + |
| 135 | + log.Printf("[DEBUG] Read active monthly billing %s", r) |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func resourceCloudProjectInstanceActiveMonthlyBillingUpdate(d *schema.ResourceData, meta interface{}) error { |
| 140 | + // nothing to do on update |
| 141 | + return resourceCloudProjectInstanceActiveMonthlyBillingRead(d, meta) |
| 142 | +} |
| 143 | + |
| 144 | +func resourceCloudProjectInstanceActiveMonthlyBillingRead(d *schema.ResourceData, meta interface{}) error { |
| 145 | + return CloudProjectInstanceActiveMonthlyBillingRead(d, meta) |
| 146 | +} |
| 147 | + |
| 148 | +func resourceCloudProjectInstanceActiveMonthlyBillingDelete(d *schema.ResourceData, meta interface{}) error { |
| 149 | + // Nothing to do on DELETE |
| 150 | + return nil |
| 151 | +} |
0 commit comments