Skip to content

Commit 353a346

Browse files
gxbenrbeuque74
andauthored
resource_cloud_project: factorize serviceName discovery and manage possible domain discrepancies (#478)
* resource_cloud_project: factorize serviceName discovery and manage possible domain discrepancies * fix: Public Cloud Project spawn handling special case in the US when multiple order details exists Sometimes, we have multiple order details, case exist when we have an HDS certification in the same order. Signed-off-by: Romain Beuque <[email protected]> --------- Signed-off-by: Romain Beuque <[email protected]> Co-authored-by: Romain Beuque <[email protected]>
1 parent e635493 commit 353a346

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

ovh/resource_cloud_project.go

+35-51
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,44 @@ func resourceCloudProjectCreate(d *schema.ResourceData, meta interface{}) error
7878
return resourceCloudProjectUpdate(d, meta)
7979
}
8080

81-
func resourceCloudProjectUpdate(d *schema.ResourceData, meta interface{}) error {
82-
order, details, err := orderRead(d, meta)
83-
if err != nil {
84-
return fmt.Errorf("Could not read cloud project order: %q", err)
81+
func resourceCloudProjectGetServiceName(config *Config, order *MeOrder, details []*MeOrderDetail) (string, error) {
82+
// Looking for an order detail associated to a Public Cloud Project.
83+
// Cloud Project has a specific resource_name that we can grep through a Regexp
84+
for _, d := range details {
85+
domain := d.Domain
86+
if publicCloudProjectNameFormatRegex.MatchString(domain) {
87+
return domain, nil
88+
}
8589
}
8690

87-
config := meta.(*Config)
88-
serviceName := details[0].Domain
89-
90-
// in the US, for reasons too long to be detailled here, cloud project order domain is not the public cloud project id, but "*".
91-
// There have been discussions to align US & EU, but they've failed.
92-
// So we end up making a few extra queries to fetch the project id from operations details.
93-
if !publicCloudProjectNameFormatRegex.MatchString(serviceName) {
94-
orderDetailId := details[0].OrderDetailId
95-
operations, err := orderDetailOperations(config.OVHClient, order.OrderId, orderDetailId)
91+
// For OVHcloud US, resource_name are not stored inside order detail, but inside the operation associated to the order detail.
92+
for _, orderDetail := range details {
93+
operations, err := orderDetailOperations(config.OVHClient, order.OrderId, orderDetail.OrderDetailId)
9694
if err != nil {
97-
return fmt.Errorf("Could not read cloudProject order details operations: %q", err)
95+
return "", fmt.Errorf("Could not read cloudProject order details operations: %q", err)
9896
}
9997
for _, operation := range operations {
100-
if !publicCloudProjectNameFormatRegex.MatchString(operation.Resource.Name) {
101-
continue
98+
if publicCloudProjectNameFormatRegex.MatchString(operation.Resource.Name) {
99+
return operation.Resource.Name, nil
102100
}
103-
serviceName = operation.Resource.Name
104101
}
105102
}
106103

104+
return "", fmt.Errorf("Unknown service name")
105+
}
106+
107+
func resourceCloudProjectUpdate(d *schema.ResourceData, meta interface{}) error {
108+
order, details, err := orderRead(d, meta)
109+
if err != nil {
110+
return fmt.Errorf("Could not read cloud project order: %q", err)
111+
}
112+
113+
config := meta.(*Config)
114+
serviceName, err := resourceCloudProjectGetServiceName(config, order, details)
115+
if err != nil {
116+
return err
117+
}
118+
107119
log.Printf("[DEBUG] Will update cloudProject: %s", serviceName)
108120
opts := (&CloudProjectUpdateOpts{}).FromResource(d)
109121
endpoint := fmt.Sprintf("/cloud/project/%s", url.PathEscape(serviceName))
@@ -121,23 +133,9 @@ func resourceCloudProjectRead(d *schema.ResourceData, meta interface{}) error {
121133
}
122134

123135
config := meta.(*Config)
124-
serviceName := details[0].Domain
125-
126-
// in the US, for reasons too long to be detailled here, cloud project order domain is not the public cloud project id, but "*".
127-
// There have been discussions to align US & EU, but they've failed.
128-
// So we end up making a few extra queries to fetch the project id from operations details.
129-
if !publicCloudProjectNameFormatRegex.MatchString(serviceName) {
130-
orderDetailId := details[0].OrderDetailId
131-
operations, err := orderDetailOperations(config.OVHClient, order.OrderId, orderDetailId)
132-
if err != nil {
133-
return fmt.Errorf("Could not read cloudProject order details operations: %q", err)
134-
}
135-
for _, operation := range operations {
136-
if !publicCloudProjectNameFormatRegex.MatchString(operation.Resource.Name) {
137-
continue
138-
}
139-
serviceName = operation.Resource.Name
140-
}
136+
serviceName, err := resourceCloudProjectGetServiceName(config, order, details)
137+
if err != nil {
138+
return err
141139
}
142140

143141
log.Printf("[DEBUG] Will read cloudProject: %s", serviceName)
@@ -162,23 +160,9 @@ func resourceCloudProjectDelete(d *schema.ResourceData, meta interface{}) error
162160
}
163161

164162
config := meta.(*Config)
165-
serviceName := details[0].Domain
166-
167-
// in the US, for reasons too long to be detailled here, cloud project order domain is not the public cloud project id, but "*".
168-
// There have been discussions to align US & EU, but they've failed.
169-
// So we end up making a few extra queries to fetch the project id from operations details.
170-
if !publicCloudProjectNameFormatRegex.MatchString(serviceName) {
171-
orderDetailId := details[0].OrderDetailId
172-
operations, err := orderDetailOperations(config.OVHClient, order.OrderId, orderDetailId)
173-
if err != nil {
174-
return fmt.Errorf("Could not read cloudProject order details operations: %q", err)
175-
}
176-
for _, operation := range operations {
177-
if !publicCloudProjectNameFormatRegex.MatchString(operation.Resource.Name) {
178-
continue
179-
}
180-
serviceName = operation.Resource.Name
181-
}
163+
serviceName, err := resourceCloudProjectGetServiceName(config, order, details)
164+
if err != nil {
165+
return err
182166
}
183167

184168
id := d.Id()

0 commit comments

Comments
 (0)