Skip to content

Commit b256116

Browse files
support gcs
1 parent 1bc51d6 commit b256116

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed

codefresh/resource_context.go

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
)
1010

1111
const (
12-
contextConfig = "config"
13-
contextSecret = "secret"
14-
contextYaml = "yaml"
15-
contextSecretYaml = "secret-yaml"
12+
contextConfig = "config"
13+
contextSecret = "secret"
14+
contextYaml = "yaml"
15+
contextSecretYaml = "secret-yaml"
16+
contextGoogleStorage = "storage.gc"
1617
)
1718

1819
var supportedContextType = []string{
@@ -135,6 +136,43 @@ func resourceContext() *schema.Resource {
135136
},
136137
},
137138
},
139+
normalizeFieldName(contextGoogleStorage): {
140+
Type: schema.TypeList,
141+
Optional: true,
142+
ForceNew: true,
143+
MaxItems: 1,
144+
ConflictsWith: getConflictingContexts(contextGoogleStorage),
145+
Elem: &schema.Resource{
146+
Schema: map[string]*schema.Schema{
147+
"data": {
148+
Type: schema.TypeList,
149+
Required: true,
150+
MaxItems: 1,
151+
Elem: &schema.Resource{
152+
Schema: map[string]*schema.Schema{
153+
"auth": {
154+
Type: schema.TypeList,
155+
Required: true,
156+
MaxItems: 1,
157+
Elem: &schema.Resource{
158+
Schema: map[string]*schema.Schema{
159+
"type": {
160+
Type: schema.TypeString,
161+
Required: true,
162+
},
163+
"json_config": {
164+
Type: schema.TypeMap,
165+
Required: true,
166+
},
167+
},
168+
},
169+
},
170+
},
171+
},
172+
},
173+
},
174+
},
175+
},
138176
},
139177
},
140178
},
@@ -239,6 +277,8 @@ func flattenContextSpec(spec cfClient.ContextSpec) []interface{} {
239277
m[normalizeFieldName(currentContextType)] = flattenContextConfig(spec)
240278
case contextYaml, contextSecretYaml:
241279
m[normalizeFieldName(currentContextType)] = flattenContextYaml(spec)
280+
case contextGoogleStorage:
281+
m[normalizeFieldName(currentContextType)] = flattenStorageContextConfig(spec)
242282
default:
243283
log.Printf("[DEBUG] Invalid context type = %v", currentContextType)
244284
return nil
@@ -256,6 +296,40 @@ func flattenContextConfig(spec cfClient.ContextSpec) []interface{} {
256296
return res
257297
}
258298

299+
func flattenStorageContextConfig(spec cfClient.ContextSpec) []interface{} {
300+
//google.[0].data[0].auth[0].[type, json]
301+
302+
var res = make([]interface{}, 0)
303+
m := make(map[string]interface{})
304+
305+
dataList := make([]interface{}, 0)
306+
data := make(map[string]interface{})
307+
308+
auth := make(map[string]interface{})
309+
auth["json_config"] = spec.Data["auth"].(map[string]interface{})["jsonConfig"]
310+
auth["type"] = spec.Data["type"]
311+
312+
authList := make([]interface{}, 0)
313+
authList = append(authList, auth)
314+
315+
data["auth"] = authList
316+
317+
dataList = append(dataList, data)
318+
319+
m["data"] = dataList
320+
res = append(res, m)
321+
return res
322+
323+
//contextData := context[0].(map[string]interface{})
324+
//contextAuth := contextData["auth"].([]interface{})[0].(map[string]interface{})
325+
//data := make(map[string]interface{})
326+
//auth := make(map[string]interface{})
327+
//auth["type"] = contextAuth["type"]
328+
//auth["jsonConfig"] = contextAuth["json_config"]
329+
//data["auth"] = auth
330+
//return data
331+
}
332+
259333
func flattenContextYaml(spec cfClient.ContextSpec) []interface{} {
260334
var res = make([]interface{}, 0)
261335
m := make(map[string]interface{})
@@ -268,11 +342,25 @@ func flattenContextYaml(spec cfClient.ContextSpec) []interface{} {
268342
return res
269343
}
270344

345+
func convertStorageContext(context []interface{}) map[string]interface{} {
346+
contextData := context[0].(map[string]interface{})
347+
contextAuth := contextData["auth"].([]interface{})[0].(map[string]interface{})
348+
data := make(map[string]interface{})
349+
auth := make(map[string]interface{})
350+
auth["type"] = contextAuth["type"]
351+
auth["jsonConfig"] = contextAuth["json_config"]
352+
data["auth"] = auth
353+
return data
354+
}
355+
271356
func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
272357

273358
var normalizedContextType string
274359
var normalizedContextData map[string]interface{}
275360

361+
spec := d.Get("spec")
362+
log.Println(spec)
363+
276364
if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextConfig) + ".0.data"); ok {
277365
normalizedContextType = contextConfig
278366
normalizedContextData = data.(map[string]interface{})
@@ -285,6 +373,9 @@ func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
285373
} else if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextSecretYaml) + ".0.data"); ok {
286374
normalizedContextType = contextSecretYaml
287375
yaml.Unmarshal([]byte(data.(string)), &normalizedContextData)
376+
} else if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextGoogleStorage) + ".0.data"); ok {
377+
normalizedContextType = contextGoogleStorage
378+
normalizedContextData = convertStorageContext(data.([]interface{}))
288379
}
289380

290381
context := &cfClient.Context{

examples/storate_integration/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//data "codefresh_context" "storage_integration" {
2+
// name = "pasha-test-t"
3+
//}
4+
5+
resource "codefresh_context" "storage_integration" {
6+
for_each = toset(["create"])
7+
name = "pasha-test-t2"
8+
spec {
9+
storagegc {
10+
data {
11+
auth {
12+
type = "basic"
13+
json_config = tomap({
14+
"fd": "fd"
15+
})
16+
}
17+
}
18+
}
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
codefresh = {
4+
source = "codefresh.io/app/codefresh"
5+
version = "0.1.0"
6+
}
7+
}
8+
}
9+
10+
provider "codefresh" {
11+
api_url = var.api_url
12+
token = var.token # If token isn't set the provider expects the $CODEFRESH_API_KEY env variable
13+
}

examples/storate_integration/vars.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
variable api_url {
2+
type = string
3+
}
4+
5+
variable token {
6+
type = string
7+
default = ""
8+
}

0 commit comments

Comments
 (0)