@@ -78,32 +78,44 @@ func resourceCloudProjectCreate(d *schema.ResourceData, meta interface{}) error
78
78
return resourceCloudProjectUpdate (d , meta )
79
79
}
80
80
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
+ }
85
89
}
86
90
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 )
96
94
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 )
98
96
}
99
97
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
102
100
}
103
- serviceName = operation .Resource .Name
104
101
}
105
102
}
106
103
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
+
107
119
log .Printf ("[DEBUG] Will update cloudProject: %s" , serviceName )
108
120
opts := (& CloudProjectUpdateOpts {}).FromResource (d )
109
121
endpoint := fmt .Sprintf ("/cloud/project/%s" , url .PathEscape (serviceName ))
@@ -121,23 +133,9 @@ func resourceCloudProjectRead(d *schema.ResourceData, meta interface{}) error {
121
133
}
122
134
123
135
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
141
139
}
142
140
143
141
log .Printf ("[DEBUG] Will read cloudProject: %s" , serviceName )
@@ -162,23 +160,9 @@ func resourceCloudProjectDelete(d *schema.ResourceData, meta interface{}) error
162
160
}
163
161
164
162
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
182
166
}
183
167
184
168
id := d .Id ()
0 commit comments