-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
134c8ee
commit 75bb5d1
Showing
6 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package ovh | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/ovh/go-ovh/ovh" | ||
"github.com/ovh/terraform-provider-ovh/ovh/helpers" | ||
) | ||
|
||
func resourceCloudProjectInstanceActiveMonthlyBilling() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudProjectInstanceActiveMonthlyBillingCreate, | ||
Update: resourceCloudProjectInstanceActiveMonthlyBillingUpdate, | ||
Read: resourceCloudProjectInstanceActiveMonthlyBillingRead, | ||
Delete: resourceCloudProjectInstanceActiveMonthlyBillingDelete, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(45 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"service_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "The internal name of your dedicated server", | ||
}, | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Public Cloud instance ID", | ||
}, | ||
"wait_activation": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "Wait for monthly billing activation", | ||
}, | ||
|
||
// Computed | ||
"monthly_billing_since": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Monthly billing activated since", | ||
}, | ||
|
||
"monthly_billing_status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Monthly billing status", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func waitForCloudProjectInstanceActiveMonthlyBillingDone(client *ovh.Client, serviceName string, instanceId string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
r := &CloudProjectInstanceActiveMonthlyBillingResponse{} | ||
endpoint := fmt.Sprintf("/cloud/project/%s/instance/%s", url.PathEscape(serviceName), url.PathEscape(instanceId)) | ||
if err := client.Get(endpoint, r); err != nil { | ||
return r, "", err | ||
} | ||
|
||
log.Printf("[DEBUG] Pending active monthly billing: %s", r) | ||
return r, r.MonthlyBilling.Status, nil | ||
} | ||
} | ||
|
||
func resourceCloudProjectInstanceActiveMonthlyBillingCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
serviceName := d.Get("service_name").(string) | ||
instanceId := d.Get("instance_id").(string) | ||
waitActivation := d.Get("wait_activation").(bool) | ||
|
||
endpoint := fmt.Sprintf( | ||
"/cloud/project/%s/instance/%s/activeMonthlyBilling", url.PathEscape(serviceName), url.PathEscape(instanceId), | ||
) | ||
|
||
params := (&CloudProjectInstanceActiveMonthlyBillingCreateOpts{}).FromResource(d) | ||
|
||
r := &CloudProjectInstanceActiveMonthlyBillingResponse{} | ||
|
||
log.Printf("[DEBUG] Will install active monthly billing: %s", params) | ||
|
||
if err := config.OVHClient.Post(endpoint, params, r); err != nil { | ||
return fmt.Errorf("Error calling POST %s:\n\t %q", endpoint, err) | ||
} | ||
|
||
if waitActivation { | ||
log.Printf("[DEBUG] Waiting for active monthly billing %s:", r) | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"activationPending"}, | ||
Target: []string{"ok"}, | ||
Refresh: waitForCloudProjectInstanceActiveMonthlyBillingDone(config.OVHClient, serviceName, instanceId), | ||
Timeout: 45 * time.Minute, | ||
Delay: 10 * time.Second, | ||
MinTimeout: 3 * time.Second, | ||
} | ||
|
||
if _, err := stateConf.WaitForState(); err != nil { | ||
return fmt.Errorf("waiting for active monthly billing (%s): %s", params, err) | ||
} | ||
} | ||
|
||
log.Printf("[DEBUG] Created active monthly billing %s", r) | ||
|
||
return CloudProjectInstanceActiveMonthlyBillingRead(d, meta) | ||
} | ||
|
||
func CloudProjectInstanceActiveMonthlyBillingRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
serviceName := d.Get("service_name").(string) | ||
instanceId := d.Get("instance_id").(string) | ||
|
||
r := &CloudProjectInstanceActiveMonthlyBillingResponse{} | ||
|
||
log.Printf("[DEBUG] Will read active monthly billing: %s", serviceName) | ||
|
||
endpoint := fmt.Sprintf("/cloud/project/%s/instance/%s", url.PathEscape(serviceName), url.PathEscape(instanceId)) | ||
|
||
if err := config.OVHClient.Get(endpoint, r); err != nil { | ||
return helpers.CheckDeleted(d, err, endpoint) | ||
} | ||
|
||
if r.MonthlyBilling != nil { | ||
d.Set("monthly_billing_since", r.MonthlyBilling.Since) | ||
d.Set("monthly_billing_status", r.MonthlyBilling.Status) | ||
} | ||
|
||
log.Printf("[DEBUG] Read active monthly billing %s", r) | ||
return nil | ||
} | ||
|
||
func resourceCloudProjectInstanceActiveMonthlyBillingUpdate(d *schema.ResourceData, meta interface{}) error { | ||
// nothing to do on update | ||
return resourceCloudProjectInstanceActiveMonthlyBillingRead(d, meta) | ||
} | ||
|
||
func resourceCloudProjectInstanceActiveMonthlyBillingRead(d *schema.ResourceData, meta interface{}) error { | ||
return CloudProjectInstanceActiveMonthlyBillingRead(d, meta) | ||
} | ||
|
||
func resourceCloudProjectInstanceActiveMonthlyBillingDelete(d *schema.ResourceData, meta interface{}) error { | ||
// Nothing to do on DELETE | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ovh | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST = "OVH_CLOUD_PROJECT_ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST" | ||
|
||
func TestAccCloudProjectInstanceActiveMonthlyBilling_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheckActiveMonthlyBilling(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCloudProjectInstanceActiveMonthlyBillingConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"ovh_cloud_project_instance_amb.monthly", "monthly_billing_status", "ok"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCloudProjectInstanceActiveMonthlyBillingConfig() string { | ||
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST") | ||
instanceId := os.Getenv(ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST) | ||
|
||
return fmt.Sprintf( | ||
testAccCloudProjectInstanceActiveMonthlyBillingConfig_Basic, | ||
serviceName, | ||
instanceId, | ||
) | ||
} | ||
|
||
const testAccCloudProjectInstanceActiveMonthlyBillingConfig_Basic = ` | ||
resource "ovh_cloud_project_instance_amb" "monthly" { | ||
service_name = "%s" | ||
instance_id = "%s" | ||
wait_activation = true | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ovh | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
type CloudProjectInstanceActiveMonthlyBillingCreateOpts struct { | ||
} | ||
|
||
func (p *CloudProjectInstanceActiveMonthlyBillingCreateOpts) String() string { | ||
return fmt.Sprintf("") | ||
} | ||
|
||
func (p *CloudProjectInstanceActiveMonthlyBillingCreateOpts) FromResource(d *schema.ResourceData) *CloudProjectInstanceActiveMonthlyBillingCreateOpts { | ||
params := &CloudProjectInstanceActiveMonthlyBillingCreateOpts{} | ||
return params | ||
} | ||
|
||
type CloudProjectInstanceActiveMonthlyBillingResponseMonthlyBilling struct { | ||
Since string `json:"since"` | ||
Status string `json:"status"` | ||
} | ||
|
||
func (p *CloudProjectInstanceActiveMonthlyBillingResponseMonthlyBilling) String() string { | ||
return fmt.Sprintf("since: %s, status: %s", p.Since, p.Status) | ||
} | ||
|
||
type CloudProjectInstanceActiveMonthlyBillingResponse struct { | ||
MonthlyBilling *CloudProjectInstanceActiveMonthlyBillingResponseMonthlyBilling `json:"monthlyBilling"` | ||
} | ||
|
||
func (p *CloudProjectInstanceActiveMonthlyBillingResponse) String() string { | ||
return fmt.Sprintf("monthlyBilling: %s", p.MonthlyBilling) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters