1
1
package codefresh
2
2
3
3
import (
4
+ storageContext "github.com/codefresh-io/terraform-provider-codefresh/codefresh/context"
4
5
"log"
5
6
6
7
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
@@ -9,10 +10,13 @@ import (
9
10
)
10
11
11
12
const (
12
- contextConfig = "config"
13
- contextSecret = "secret"
14
- contextYaml = "yaml"
15
- contextSecretYaml = "secret-yaml"
13
+ contextConfig = "config"
14
+ contextSecret = "secret"
15
+ contextYaml = "yaml"
16
+ contextSecretYaml = "secret-yaml"
17
+ contextGoogleStorage = "storage.gc"
18
+ contextS3Storage = "storage.s3"
19
+ contextAzureStorage = "storage.azuref"
16
20
)
17
21
18
22
var supportedContextType = []string {
@@ -135,6 +139,9 @@ func resourceContext() *schema.Resource {
135
139
},
136
140
},
137
141
},
142
+ normalizeFieldName (contextGoogleStorage ): storageContext .GcsSchema (),
143
+ normalizeFieldName (contextS3Storage ): storageContext .S3Schema (),
144
+ normalizeFieldName (contextAzureStorage ): storageContext .AzureStorage (),
138
145
},
139
146
},
140
147
},
@@ -145,8 +152,7 @@ func resourceContext() *schema.Resource {
145
152
func resourceContextCreate (d * schema.ResourceData , meta interface {}) error {
146
153
147
154
client := meta .(* cfClient.Client )
148
- context := * mapResourceToContext (d )
149
- resp , err := client .CreateContext (& context )
155
+ resp , err := client .CreateContext (mapResourceToContext (d ))
150
156
if err != nil {
151
157
log .Printf ("[DEBUG] Error while creating context. Error = %v" , err )
152
158
return err
@@ -222,10 +228,6 @@ func mapContextToResource(context cfClient.Context, d *schema.ResourceData) erro
222
228
return err
223
229
}
224
230
225
- if err != nil {
226
- return err
227
- }
228
-
229
231
return nil
230
232
}
231
233
@@ -239,6 +241,10 @@ func flattenContextSpec(spec cfClient.ContextSpec) []interface{} {
239
241
m [normalizeFieldName (currentContextType )] = flattenContextConfig (spec )
240
242
case contextYaml , contextSecretYaml :
241
243
m [normalizeFieldName (currentContextType )] = flattenContextYaml (spec )
244
+ case contextGoogleStorage , contextS3Storage :
245
+ m [normalizeFieldName (currentContextType )] = storageContext .FlattenJsonConfigStorageContextConfig (spec )
246
+ case contextAzureStorage :
247
+ m [normalizeFieldName (currentContextType )] = storageContext .FlattenAzureStorageContextConfig (spec )
242
248
default :
243
249
log .Printf ("[DEBUG] Invalid context type = %v" , currentContextType )
244
250
return nil
@@ -281,13 +287,22 @@ func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
281
287
normalizedContextData = data .(map [string ]interface {})
282
288
} else if data , ok := d .GetOk ("spec.0." + normalizeFieldName (contextYaml ) + ".0.data" ); ok {
283
289
normalizedContextType = contextYaml
284
- yaml .Unmarshal ([]byte (data .(string )), & normalizedContextData )
290
+ _ = yaml .Unmarshal ([]byte (data .(string )), & normalizedContextData )
285
291
} else if data , ok := d .GetOk ("spec.0." + normalizeFieldName (contextSecretYaml ) + ".0.data" ); ok {
286
292
normalizedContextType = contextSecretYaml
287
- yaml .Unmarshal ([]byte (data .(string )), & normalizedContextData )
293
+ _ = yaml .Unmarshal ([]byte (data .(string )), & normalizedContextData )
294
+ } else if data , ok := d .GetOk ("spec.0." + normalizeFieldName (contextGoogleStorage ) + ".0.data" ); ok {
295
+ normalizedContextType = contextGoogleStorage
296
+ normalizedContextData = storageContext .ConvertJsonConfigStorageContext (data .([]interface {}))
297
+ } else if data , ok := d .GetOk ("spec.0." + normalizeFieldName (contextS3Storage ) + ".0.data" ); ok {
298
+ normalizedContextType = contextS3Storage
299
+ normalizedContextData = storageContext .ConvertJsonConfigStorageContext (data .([]interface {}))
300
+ } else if data , ok := d .GetOk ("spec.0." + normalizeFieldName (contextAzureStorage ) + ".0.data" ); ok {
301
+ normalizedContextType = contextAzureStorage
302
+ normalizedContextData = storageContext .ConvertAzureStorageContext (data .([]interface {}))
288
303
}
289
304
290
- context := & cfClient.Context {
305
+ return & cfClient.Context {
291
306
Metadata : cfClient.ContextMetadata {
292
307
Name : d .Get ("name" ).(string ),
293
308
},
@@ -296,6 +311,4 @@ func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
296
311
Data : normalizedContextData ,
297
312
},
298
313
}
299
-
300
- return context
301
314
}
0 commit comments