11package codefresh
22
33import (
4+ storageContext "github.com/codefresh-io/terraform-provider-codefresh/codefresh/context"
45 "log"
56
67 cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
@@ -9,10 +10,13 @@ import (
910)
1011
1112const (
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"
1620)
1721
1822var supportedContextType = []string {
@@ -135,6 +139,9 @@ func resourceContext() *schema.Resource {
135139 },
136140 },
137141 },
142+ normalizeFieldName (contextGoogleStorage ): storageContext .GcsSchema (),
143+ normalizeFieldName (contextS3Storage ): storageContext .S3Schema (),
144+ normalizeFieldName (contextAzureStorage ): storageContext .AzureStorage (),
138145 },
139146 },
140147 },
@@ -145,8 +152,7 @@ func resourceContext() *schema.Resource {
145152func resourceContextCreate (d * schema.ResourceData , meta interface {}) error {
146153
147154 client := meta .(* cfClient.Client )
148- context := * mapResourceToContext (d )
149- resp , err := client .CreateContext (& context )
155+ resp , err := client .CreateContext (mapResourceToContext (d ))
150156 if err != nil {
151157 log .Printf ("[DEBUG] Error while creating context. Error = %v" , err )
152158 return err
@@ -222,10 +228,6 @@ func mapContextToResource(context cfClient.Context, d *schema.ResourceData) erro
222228 return err
223229 }
224230
225- if err != nil {
226- return err
227- }
228-
229231 return nil
230232}
231233
@@ -239,6 +241,10 @@ func flattenContextSpec(spec cfClient.ContextSpec) []interface{} {
239241 m [normalizeFieldName (currentContextType )] = flattenContextConfig (spec )
240242 case contextYaml , contextSecretYaml :
241243 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 )
242248 default :
243249 log .Printf ("[DEBUG] Invalid context type = %v" , currentContextType )
244250 return nil
@@ -281,13 +287,22 @@ func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
281287 normalizedContextData = data .(map [string ]interface {})
282288 } else if data , ok := d .GetOk ("spec.0." + normalizeFieldName (contextYaml ) + ".0.data" ); ok {
283289 normalizedContextType = contextYaml
284- yaml .Unmarshal ([]byte (data .(string )), & normalizedContextData )
290+ _ = yaml .Unmarshal ([]byte (data .(string )), & normalizedContextData )
285291 } else if data , ok := d .GetOk ("spec.0." + normalizeFieldName (contextSecretYaml ) + ".0.data" ); ok {
286292 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 {}))
288303 }
289304
290- context := & cfClient.Context {
305+ return & cfClient.Context {
291306 Metadata : cfClient.ContextMetadata {
292307 Name : d .Get ("name" ).(string ),
293308 },
@@ -296,6 +311,4 @@ func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
296311 Data : normalizedContextData ,
297312 },
298313 }
299-
300- return context
301314}
0 commit comments