Skip to content

Commit a8579c5

Browse files
Merge pull request #65 from codefresh-io/CR-5752
Cr 5752
2 parents 1bc51d6 + 8052387 commit a8579c5

File tree

6 files changed

+261
-16
lines changed

6 files changed

+261
-16
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ dist/
44
**/.terraform
55
**/terraform.tfstate
66
**/terraform.tfstate.backup
7-
tests/
7+
tests/
8+
9+
.idea
10+
**/*.lock.hcl
11+
**/*.backup

codefresh/context/storage.go

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package context
2+
3+
import (
4+
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
6+
)
7+
8+
func convertStorageContext(context []interface{}, auth map[string]interface{}) map[string]interface{} {
9+
data := make(map[string]interface{})
10+
data["auth"] = auth
11+
return data
12+
}
13+
14+
func ConvertJsonConfigStorageContext(context []interface{}) map[string]interface{} {
15+
contextData := context[0].(map[string]interface{})
16+
contextAuth := contextData["auth"].([]interface{})[0].(map[string]interface{})
17+
auth := make(map[string]interface{})
18+
auth["type"] = contextAuth["type"]
19+
auth["jsonConfig"] = contextAuth["json_config"]
20+
return convertStorageContext(context, auth)
21+
}
22+
23+
func ConvertAzureStorageContext(context []interface{}) map[string]interface{} {
24+
contextData := context[0].(map[string]interface{})
25+
contextAuth := contextData["auth"].([]interface{})[0].(map[string]interface{})
26+
auth := make(map[string]interface{})
27+
auth["type"] = contextAuth["type"]
28+
auth["accountName"] = contextAuth["account_name"]
29+
auth["accountKey"] = contextAuth["account_key"]
30+
return convertStorageContext(context, auth)
31+
}
32+
33+
func flattenStorageContextConfig(spec cfClient.ContextSpec, auth map[string]interface{}) []interface{} {
34+
35+
var res = make([]interface{}, 0)
36+
m := make(map[string]interface{})
37+
38+
dataList := make([]interface{}, 0)
39+
data := make(map[string]interface{})
40+
41+
authList := make([]interface{}, 0)
42+
authList = append(authList, auth)
43+
44+
data["auth"] = authList
45+
46+
dataList = append(dataList, data)
47+
48+
m["data"] = dataList
49+
res = append(res, m)
50+
return res
51+
52+
}
53+
54+
func FlattenJsonConfigStorageContextConfig(spec cfClient.ContextSpec) []interface{} {
55+
auth := make(map[string]interface{})
56+
auth["json_config"] = spec.Data["auth"].(map[string]interface{})["jsonConfig"]
57+
auth["type"] = spec.Data["type"]
58+
return flattenStorageContextConfig(spec, auth)
59+
}
60+
61+
func FlattenAzureStorageContextConfig(spec cfClient.ContextSpec) []interface{} {
62+
auth := make(map[string]interface{})
63+
authParams := spec.Data["auth"].(map[string]interface{})
64+
auth["account_name"] = authParams["accountName"]
65+
auth["account_key"] = authParams["accountKey"]
66+
auth["type"] = spec.Data["type"]
67+
return flattenStorageContextConfig(spec, auth)
68+
}
69+
70+
func storageSchema(authSchema *schema.Schema) *schema.Schema {
71+
return &schema.Schema{
72+
Type: schema.TypeList,
73+
Optional: true,
74+
ForceNew: true,
75+
MaxItems: 1,
76+
Elem: &schema.Resource{
77+
Schema: map[string]*schema.Schema{
78+
"data": {
79+
Type: schema.TypeList,
80+
Required: true,
81+
MaxItems: 1,
82+
Elem: &schema.Resource{
83+
Schema: map[string]*schema.Schema{
84+
"auth": authSchema,
85+
},
86+
},
87+
},
88+
},
89+
},
90+
}
91+
}
92+
93+
func GcsSchema() *schema.Schema {
94+
sch := &schema.Schema{
95+
Type: schema.TypeList,
96+
Required: true,
97+
MaxItems: 1,
98+
Elem: &schema.Resource{
99+
Schema: map[string]*schema.Schema{
100+
"type": {
101+
Type: schema.TypeString,
102+
Required: true,
103+
},
104+
"json_config": {
105+
Type: schema.TypeMap,
106+
Required: true,
107+
},
108+
},
109+
},
110+
}
111+
return storageSchema(sch)
112+
}
113+
114+
func S3Schema() *schema.Schema {
115+
sch := &schema.Schema{
116+
Type: schema.TypeList,
117+
Required: true,
118+
MaxItems: 1,
119+
Elem: &schema.Resource{
120+
Schema: map[string]*schema.Schema{
121+
"type": {
122+
Type: schema.TypeString,
123+
Required: true,
124+
},
125+
"json_config": {
126+
Type: schema.TypeMap,
127+
Required: true,
128+
},
129+
},
130+
},
131+
}
132+
return storageSchema(sch)
133+
}
134+
135+
func AzureStorage() *schema.Schema {
136+
sch := &schema.Schema{
137+
Type: schema.TypeList,
138+
Required: true,
139+
MaxItems: 1,
140+
Elem: &schema.Resource{
141+
Schema: map[string]*schema.Schema{
142+
"type": {
143+
Type: schema.TypeString,
144+
Required: true,
145+
},
146+
"account_name": {
147+
Type: schema.TypeString,
148+
Required: true,
149+
},
150+
"account_key": {
151+
Type: schema.TypeString,
152+
Required: true,
153+
},
154+
},
155+
},
156+
}
157+
return storageSchema(sch)
158+
}

codefresh/resource_context.go

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codefresh
22

33
import (
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

1112
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"
1620
)
1721

1822
var 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 {
145152
func 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
}

examples/storate_integration/main.tf

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
resource "codefresh_context" "gcs" {
2+
for_each = toset(["create"])
3+
name = "gcs"
4+
spec {
5+
storagegc {
6+
data {
7+
auth {
8+
type = "basic"
9+
json_config = tomap({
10+
"config": "cf"
11+
})
12+
}
13+
}
14+
}
15+
}
16+
}
17+
18+
resource "codefresh_context" "s3" {
19+
for_each = toset(["create"])
20+
name = "s3"
21+
spec {
22+
storages3 {
23+
data {
24+
auth {
25+
type = "basic"
26+
json_config = tomap({
27+
"config": "cf"
28+
})
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
resource "codefresh_context" "azure" {
36+
for_each = toset(["create"])
37+
name = "azure"
38+
spec {
39+
storageazuref {
40+
data {
41+
auth {
42+
type = "basic"
43+
account_name = "accName"
44+
account_key = "accKey"
45+
}
46+
}
47+
}
48+
}
49+
}
+13
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

+8
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)