Skip to content
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

Introducing support for Contexts (Config, Secret,Yaml, SecretYaml) #27

Merged
merged 2 commits into from
Jan 28, 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
132 changes: 132 additions & 0 deletions client/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package client

import (
"fmt"
"log"
"net/url"
)

type ContextErrorResponse struct {
Status int `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}

type ContextMetadata struct {
Name string `json:"name,omitempty"`
}

type Context struct {
Metadata ContextMetadata `json:"metadata,omitempty"`
Spec ContextSpec `json:"spec,omitempty"`
Version string `json:"version,omitempty"`
}

type ContextSpec struct {
Type string `json:"type,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}

func (context *Context) GetID() string {
return context.Metadata.Name
}

func (client *Client) GetContext(name string) (*Context, error) {
fullPath := fmt.Sprintf("/contexts/%s?decrypt=true", url.PathEscape(name))
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}
var respContext Context
err = DecodeResponseInto(resp, &respContext)
if err != nil {
return nil, err
}

return &respContext, nil

}

func (client *Client) CreateContext(context *Context) (*Context, error) {

body, err := EncodeToJSON(context)

if err != nil {
return nil, err
}
opts := RequestOptions{
Path: "/contexts",
Method: "POST",
Body: body,
}

resp, err := client.RequestAPI(&opts)
log.Printf("[DEBUG] Called API for context with Body %v", body)
if err != nil {
log.Printf("[DEBUG] Call to API for context creation failed with Error = %v for Body %v", err, body)
return nil, err
}

var respContext Context
err = DecodeResponseInto(resp, &respContext)
if err != nil {
return nil, err
}

return &respContext, nil

}

func (client *Client) UpdateContext(context *Context) (*Context, error) {

body, err := EncodeToJSON(context)

if err != nil {
return nil, err
}

fullPath := fmt.Sprintf("/contexts/%s", url.PathEscape(context.Metadata.Name))
opts := RequestOptions{
Path: fullPath,
Method: "PUT",
Body: body,
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var respContext Context
err = DecodeResponseInto(resp, &respContext)
if err != nil {
return nil, err
}

return &respContext, nil

}

func (client *Client) DeleteContext(name string) error {

fullPath := fmt.Sprintf("/contexts/%s", url.PathEscape(name))
opts := RequestOptions{
Path: fullPath,
Method: "DELETE",
}

_, err := client.RequestAPI(&opts)

if err != nil {
return err
}

return nil
}
69 changes: 69 additions & 0 deletions codefresh/data_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package codefresh

import (
"fmt"

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

func dataSourceContext() *schema.Resource {
return &schema.Resource{
Read: dataSourceContextRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"data": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceContextRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
var context *cfClient.Context
var err error

if name, nameOk := d.GetOk("name"); nameOk {
context, err = client.GetContext(name.(string))
} else {
return fmt.Errorf("data.codefresh_context - must specify name")
}
if err != nil {
return err
}

if context == nil {
return fmt.Errorf("data.codefresh_context - cannot find context")
}

return mapDataContextToResource(context, d)
}

func mapDataContextToResource(context *cfClient.Context, d *schema.ResourceData) error {

if context == nil || context.Metadata.Name == "" {
return fmt.Errorf("data.codefresh_context - failed to mapDataContextToResource")
}
d.SetId(context.Metadata.Name)

d.Set("name", context.Metadata.Name)
d.Set("type", context.Spec.Type)
data, err := yaml.Marshal(context.Spec.Data)
if err != nil {
return err
}
d.Set("data", string(data))

return nil
}
3 changes: 3 additions & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codefresh
import (
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

//"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"os"
)
Expand Down Expand Up @@ -32,10 +33,12 @@ func Provider() *schema.Provider {
"codefresh_account": dataSourceAccount(),
"codefresh_team": dataSourceTeam(),
"codefresh_current_account": dataSourceCurrentAccount(),
"codefresh_context": dataSourceContext(),
},
ResourcesMap: map[string]*schema.Resource{
"codefresh_project": resourceProject(),
"codefresh_pipeline": resourcePipeline(),
"codefresh_context": resourceContext(),
"codefresh_team": resourceTeam(),
"codefresh_account": resourceAccount(),
"codefresh_api_key": resourceApiKey(),
Expand Down
Loading