Skip to content

Commit 85f9815

Browse files
committed
feat: cloud provider instance amb
1 parent a1637ef commit 85f9815

6 files changed

+242
-0
lines changed

ovh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func Provider() *schema.Provider {
150150
"ovh_cloud_project_database_redis_user": resourceCloudProjectDatabaseRedisUser(),
151151
"ovh_cloud_project_database_user": resourceCloudProjectDatabaseUser(),
152152
"ovh_cloud_project_failover_ip_attach": resourceCloudProjectFailoverIpAttach(),
153+
"ovh_cloud_project_instance_amb": resourceCloudProjectInstanceActiveMonthlyBilling(),
153154
"ovh_cloud_project_kube": resourceCloudProjectKube(),
154155
"ovh_cloud_project_kube_nodepool": resourceCloudProjectKubeNodePool(),
155156
"ovh_cloud_project_kube_oidc": resourceCloudProjectKubeOIDC(),

ovh/provider_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,8 @@ func testAccPreCheckWorkflowBackup(t *testing.T) {
351351
checkEnvOrSkip(t, WORKFLOW_BACKUP_TEST_INSTANCE_ID_ENV_VAR)
352352
checkEnvOrSkip(t, WORKFLOW_BACKUP_TEST_REGION_ENV_VAR)
353353
}
354+
355+
func testAccPreCheckActiveMonthlyBilling(t *testing.T) {
356+
testAccPreCheckCloud(t)
357+
checkEnvOrSkip(t, ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST)
358+
}
+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
const ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST = "OVH_CLOUD_PROJECT_ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST"
12+
13+
func TestAccCloudProjectInstanceActiveMonthlyBilling_basic(t *testing.T) {
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() {
16+
testAccPreCheckActiveMonthlyBilling(t)
17+
},
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccCloudProjectInstanceActiveMonthlyBillingConfig(),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttr(
24+
"ovh_cloud_project_instance_amb.monthly", "monthly_billing_status", "ok"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccCloudProjectInstanceActiveMonthlyBillingConfig() string {
32+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
33+
instanceId := os.Getenv(ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST)
34+
35+
return fmt.Sprintf(
36+
testAccCloudProjectInstanceActiveMonthlyBillingConfig_Basic,
37+
serviceName,
38+
instanceId,
39+
)
40+
}
41+
42+
const testAccCloudProjectInstanceActiveMonthlyBillingConfig_Basic = `
43+
resource "ovh_cloud_project_instance_amb" "monthly" {
44+
service_name = "%s"
45+
instance_id = "%s"
46+
wait_activation = true
47+
}
48+
`
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
type CloudProjectInstanceActiveMonthlyBillingCreateOpts struct {
10+
}
11+
12+
func (p *CloudProjectInstanceActiveMonthlyBillingCreateOpts) String() string {
13+
return fmt.Sprintf("")
14+
}
15+
16+
func (p *CloudProjectInstanceActiveMonthlyBillingCreateOpts) FromResource(d *schema.ResourceData) *CloudProjectInstanceActiveMonthlyBillingCreateOpts {
17+
params := &CloudProjectInstanceActiveMonthlyBillingCreateOpts{}
18+
return params
19+
}
20+
21+
type CloudProjectInstanceActiveMonthlyBillingResponseMonthlyBilling struct {
22+
Since string `json:"since"`
23+
Status string `json:"status"`
24+
}
25+
26+
func (p *CloudProjectInstanceActiveMonthlyBillingResponseMonthlyBilling) String() string {
27+
return fmt.Sprintf("since: %s, status: %s", p.Since, p.Status)
28+
}
29+
30+
type CloudProjectInstanceActiveMonthlyBillingResponse struct {
31+
MonthlyBilling CloudProjectInstanceActiveMonthlyBillingResponseMonthlyBilling `json:"monthlyBilling"`
32+
}
33+
34+
func (p *CloudProjectInstanceActiveMonthlyBillingResponse) String() string {
35+
return fmt.Sprintf("monthlyBilling: %s", p.MonthlyBilling)
36+
}

website/docs/index.html.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ variables must also be set:
191191

192192
* `OVH_CLOUD_PROJECT_WORKFLOW_BACKUP_REGION_TEST` - The openstack region in which the workflow will be defined
193193
* `OVH_CLOUD_PROJECT_WORKFLOW_BACKUP_INSTANCE_ID_TEST` - The openstack id of the instance to backup
194+
* `OVH_CLOUD_PROJECT_ACTIVE_MONTHLY_BILLING_INSTANCE_ID_TEST` - The openstack id of the instance to activate monthly billing
194195

195196
### Used by OVHcloud internal account only:
196197

0 commit comments

Comments
 (0)