Skip to content

Cr 5752 #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ dist/
**/.terraform
**/terraform.tfstate
**/terraform.tfstate.backup
tests/
tests/

.idea
**/*.lock.hcl
**/*.backup
158 changes: 158 additions & 0 deletions codefresh/context/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package context

import (
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func convertStorageContext(context []interface{}, auth map[string]interface{}) map[string]interface{} {
data := make(map[string]interface{})
data["auth"] = auth
return data
}

func ConvertJsonConfigStorageContext(context []interface{}) map[string]interface{} {
contextData := context[0].(map[string]interface{})
contextAuth := contextData["auth"].([]interface{})[0].(map[string]interface{})
auth := make(map[string]interface{})
auth["type"] = contextAuth["type"]
auth["jsonConfig"] = contextAuth["json_config"]
return convertStorageContext(context, auth)
}

func ConvertAzureStorageContext(context []interface{}) map[string]interface{} {
contextData := context[0].(map[string]interface{})
contextAuth := contextData["auth"].([]interface{})[0].(map[string]interface{})
auth := make(map[string]interface{})
auth["type"] = contextAuth["type"]
auth["accountName"] = contextAuth["account_name"]
auth["accountKey"] = contextAuth["account_key"]
return convertStorageContext(context, auth)
}

func flattenStorageContextConfig(spec cfClient.ContextSpec, auth map[string]interface{}) []interface{} {

var res = make([]interface{}, 0)
m := make(map[string]interface{})

dataList := make([]interface{}, 0)
data := make(map[string]interface{})

authList := make([]interface{}, 0)
authList = append(authList, auth)

data["auth"] = authList

dataList = append(dataList, data)

m["data"] = dataList
res = append(res, m)
return res

}

func FlattenJsonConfigStorageContextConfig(spec cfClient.ContextSpec) []interface{} {
auth := make(map[string]interface{})
auth["json_config"] = spec.Data["auth"].(map[string]interface{})["jsonConfig"]
auth["type"] = spec.Data["type"]
return flattenStorageContextConfig(spec, auth)
}

func FlattenAzureStorageContextConfig(spec cfClient.ContextSpec) []interface{} {
auth := make(map[string]interface{})
authParams := spec.Data["auth"].(map[string]interface{})
auth["account_name"] = authParams["accountName"]
auth["account_key"] = authParams["accountKey"]
auth["type"] = spec.Data["type"]
return flattenStorageContextConfig(spec, auth)
}

func storageSchema(authSchema *schema.Schema) *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auth": authSchema,
},
},
},
},
},
}
}

func GcsSchema() *schema.Schema {
sch := &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"json_config": {
Type: schema.TypeMap,
Required: true,
},
},
},
}
return storageSchema(sch)
}

func S3Schema() *schema.Schema {
sch := &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"json_config": {
Type: schema.TypeMap,
Required: true,
},
},
},
}
return storageSchema(sch)
}

func AzureStorage() *schema.Schema {
sch := &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"account_name": {
Type: schema.TypeString,
Required: true,
},
"account_key": {
Type: schema.TypeString,
Required: true,
},
},
},
}
return storageSchema(sch)
}
43 changes: 28 additions & 15 deletions codefresh/resource_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codefresh

import (
storageContext "github.com/codefresh-io/terraform-provider-codefresh/codefresh/context"
"log"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
Expand All @@ -9,10 +10,13 @@ import (
)

const (
contextConfig = "config"
contextSecret = "secret"
contextYaml = "yaml"
contextSecretYaml = "secret-yaml"
contextConfig = "config"
contextSecret = "secret"
contextYaml = "yaml"
contextSecretYaml = "secret-yaml"
contextGoogleStorage = "storage.gc"
contextS3Storage = "storage.s3"
contextAzureStorage = "storage.azuref"
)

var supportedContextType = []string{
Expand Down Expand Up @@ -135,6 +139,9 @@ func resourceContext() *schema.Resource {
},
},
},
normalizeFieldName(contextGoogleStorage): storageContext.GcsSchema(),
normalizeFieldName(contextS3Storage): storageContext.S3Schema(),
normalizeFieldName(contextAzureStorage): storageContext.AzureStorage(),
},
},
},
Expand All @@ -145,8 +152,7 @@ func resourceContext() *schema.Resource {
func resourceContextCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
context := *mapResourceToContext(d)
resp, err := client.CreateContext(&context)
resp, err := client.CreateContext(mapResourceToContext(d))
if err != nil {
log.Printf("[DEBUG] Error while creating context. Error = %v", err)
return err
Expand Down Expand Up @@ -222,10 +228,6 @@ func mapContextToResource(context cfClient.Context, d *schema.ResourceData) erro
return err
}

if err != nil {
return err
}

return nil
}

Expand All @@ -239,6 +241,10 @@ func flattenContextSpec(spec cfClient.ContextSpec) []interface{} {
m[normalizeFieldName(currentContextType)] = flattenContextConfig(spec)
case contextYaml, contextSecretYaml:
m[normalizeFieldName(currentContextType)] = flattenContextYaml(spec)
case contextGoogleStorage, contextS3Storage:
m[normalizeFieldName(currentContextType)] = storageContext.FlattenJsonConfigStorageContextConfig(spec)
case contextAzureStorage:
m[normalizeFieldName(currentContextType)] = storageContext.FlattenAzureStorageContextConfig(spec)
default:
log.Printf("[DEBUG] Invalid context type = %v", currentContextType)
return nil
Expand Down Expand Up @@ -281,13 +287,22 @@ func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
normalizedContextData = data.(map[string]interface{})
} else if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextYaml) + ".0.data"); ok {
normalizedContextType = contextYaml
yaml.Unmarshal([]byte(data.(string)), &normalizedContextData)
_ = yaml.Unmarshal([]byte(data.(string)), &normalizedContextData)
} else if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextSecretYaml) + ".0.data"); ok {
normalizedContextType = contextSecretYaml
yaml.Unmarshal([]byte(data.(string)), &normalizedContextData)
_ = yaml.Unmarshal([]byte(data.(string)), &normalizedContextData)
} else if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextGoogleStorage) + ".0.data"); ok {
normalizedContextType = contextGoogleStorage
normalizedContextData = storageContext.ConvertJsonConfigStorageContext(data.([]interface{}))
} else if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextS3Storage) + ".0.data"); ok {
normalizedContextType = contextS3Storage
normalizedContextData = storageContext.ConvertJsonConfigStorageContext(data.([]interface{}))
} else if data, ok := d.GetOk("spec.0." + normalizeFieldName(contextAzureStorage) + ".0.data"); ok {
normalizedContextType = contextAzureStorage
normalizedContextData = storageContext.ConvertAzureStorageContext(data.([]interface{}))
}

context := &cfClient.Context{
return &cfClient.Context{
Metadata: cfClient.ContextMetadata{
Name: d.Get("name").(string),
},
Expand All @@ -296,6 +311,4 @@ func mapResourceToContext(d *schema.ResourceData) *cfClient.Context {
Data: normalizedContextData,
},
}

return context
}
49 changes: 49 additions & 0 deletions examples/storate_integration/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
resource "codefresh_context" "gcs" {
for_each = toset(["create"])
name = "gcs"
spec {
storagegc {
data {
auth {
type = "basic"
json_config = tomap({
"config": "cf"
})
}
}
}
}
}

resource "codefresh_context" "s3" {
for_each = toset(["create"])
name = "s3"
spec {
storages3 {
data {
auth {
type = "basic"
json_config = tomap({
"config": "cf"
})
}
}
}
}
}

resource "codefresh_context" "azure" {
for_each = toset(["create"])
name = "azure"
spec {
storageazuref {
data {
auth {
type = "basic"
account_name = "accName"
account_key = "accKey"
}
}
}
}
}
13 changes: 13 additions & 0 deletions examples/storate_integration/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
codefresh = {
source = "codefresh.io/app/codefresh"
version = "0.1.0"
}
}
}

provider "codefresh" {
api_url = var.api_url
token = var.token # If token isn't set the provider expects the $CODEFRESH_API_KEY env variable
}
8 changes: 8 additions & 0 deletions examples/storate_integration/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
variable api_url {
type = string
}

variable token {
type = string
default = ""
}