Skip to content

Commit

Permalink
Add client
Browse files Browse the repository at this point in the history
  • Loading branch information
palson-cf committed May 18, 2020
1 parent 17d4c61 commit 7eada8d
Show file tree
Hide file tree
Showing 5 changed files with 664 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
// "net/http"
)

type Config struct {
APIServer string
Token string
}
203 changes: 203 additions & 0 deletions todo/resource_cron_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package main

import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"net/url"
"strings"
)

type cronEventResp struct {
Type string `json:"type,omitempty"`
Kind string `json:"kind,omitempty"`
Secret string `json:"secret,omitempty"`
URI string `json:"uri,omitempty"`
}

func (r *cronEventResp) toCronEvent() *cronEvent {
parts := strings.Split(r.URI, ":")
return &cronEvent{
Type: parts[0],
Kind: parts[1],
Secret: parts[4],
Values: cronEventValues{
Message: parts[3],
Expression: parts[2],
},
}
}

type cronEvent struct {
Type string `json:"type,omitempty"`
Kind string `json:"kind,omitempty"`
Secret string `json:"secret,omitempty"`
Values cronEventValues `json:"values,omitempty"`
}

type cronEventValues struct {
Expression string `json:"expression,omitempty"`
Message string `json:"message,omitempty"`
}

func (e *cronEvent) getID() string {
return fmt.Sprintf("%v:%v:%v:%v:%v", e.Type, e.Kind, e.Values.Expression, e.Values.Message, e.Secret)
}

func (e *cronEvent) urlEncode() string {
return urlEncode(e.getID())
}

func urlEncode(str string) string {
return url.QueryEscape(url.QueryEscape(str))
}

func resourceCronEvent() *schema.Resource {
return &schema.Resource{
Create: resourceCronEventCreate,
Read: resourceCronEventRead,
Delete: resourceCronEventDelete,
Importer: &schema.ResourceImporter{
State: resourceCronEventImport,
},
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Default: "cron",
ForceNew: true,
},
"kind": {
Type: schema.TypeString,
Optional: true,
Default: "codefresh",
ForceNew: true,
},
"secret": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"expression": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"message": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceCronEventCreate(d *schema.ResourceData, _ interface{}) error {
return createCodefreshObject(
fmt.Sprintf("%v/hermes/events", getCfUrl()),
"POST",
d,
mapResourceToCronEvent,
readCronEventString,
)
}

func resourceCronEventRead(d *schema.ResourceData, _ interface{}) error {
return readCodefreshObject(
d,
getCronEventFromCodefresh,
mapCronEventToResource)
}

// TODO: I don't think this is actually deleting anything, I have an open ticket with Codefresh
// https://support.codefresh.io/hc/en-us/requests/3167?page=1
func resourceCronEventDelete(d *schema.ResourceData, _ interface{}) error {
cfURL := fmt.Sprintf("%v/hermes/events/%v", getCfUrl(), urlEncode(d.Id()))
return deleteCodefreshObject(cfURL)
}

func resourceCronEventImport(d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
return importCodefreshObject(
d,
getCronEventFromCodefresh,
mapCronEventToResource)
}

func mapResourceToCronEvent(d *schema.ResourceData) codefreshObject {
dSecret := d.Get("secret").(string)
if dSecret == "" {
dSecret = "!generate"
}
cronEvent := &cronEvent{
Type: d.Get("type").(string),
Kind: d.Get("kind").(string),
Secret: dSecret,
Values: cronEventValues{
Expression: d.Get("expression").(string),
Message: d.Get("message").(string),
},
}
return cronEvent
}

// readCronEventString reads a simple string response as is returned from CREATE
func readCronEventString(_ *schema.ResourceData, b []byte) (codefreshObject, error) {
var respString string
err := json.Unmarshal(b, &respString)
if err != nil {
return nil, err
}
cronEventResp := &cronEventResp{
URI: respString,
}
return cronEventResp.toCronEvent(), nil
}

// readCronEvent reads a JSON type response as is returned from GET
func readCronEvent(_ *schema.ResourceData, b []byte) (codefreshObject, error) {
cronEventResp := &cronEventResp{}

err := json.Unmarshal(b, cronEventResp)
if err != nil {
return nil, err
}
return cronEventResp.toCronEvent(), nil
}

func getCronEventFromCodefresh(d *schema.ResourceData) (codefreshObject, error) {
// get the event
event := d.Id()
cfURL := fmt.Sprintf("%v/hermes/events/%v", getCfUrl(), urlEncode(event))
return getFromCodefresh(d, cfURL, readCronEvent)
}

func mapCronEventToResource(cfObject codefreshObject, d *schema.ResourceData) error {
cronEvent := cfObject.(*cronEvent)
d.SetId(cronEvent.getID())

err := d.Set("type", cronEvent.Type)
if err != nil {
return err
}

err = d.Set("kind", cronEvent.Kind)
if err != nil {
return err
}

err = d.Set("secret", cronEvent.Secret)
if err != nil {
return err
}

err = d.Set("message", cronEvent.Values.Message)
if err != nil {
return err
}

err = d.Set("expression", cronEvent.Values.Expression)
if err != nil {
return err
}
return nil
}
125 changes: 125 additions & 0 deletions todo/resource_cron_trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

type cronTrigger struct {
Pipeline string `json:"pipeline,omitempty"`
Event string `json:"event,omitempty"`
}

func (t *cronTrigger) getID() string {
return t.Pipeline
}

func resourceCronTrigger() *schema.Resource {
return &schema.Resource{
Create: resourceCronTriggerCreate,
Read: resourceCronTriggerRead,
Delete: resourceCronTriggerDelete,
Importer: &schema.ResourceImporter{
State: resourceCronTriggerImport,
},
Schema: map[string]*schema.Schema{
"pipeline": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"event": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceCronTriggerCreate(d *schema.ResourceData, _ interface{}) error {
return createCodefreshObject(
fmt.Sprintf("%v/hermes/triggers/%v/%v", getCfUrl(), urlEncode(d.Get("event").(string)), d.Get("pipeline")),
"POST",
d,
mapResourceToCronTrigger,
readCronTriggerString,
)
}

func resourceCronTriggerRead(d *schema.ResourceData, _ interface{}) error {
return readCodefreshObject(
d,
getCronTriggerFromCodefresh,
mapCronTriggerToResource)
}

// TODO: I don't think this is actually deleting anything, I have an open ticket with Codefresh
// https://support.codefresh.io/hc/en-us/requests/3167?page=1
func resourceCronTriggerDelete(d *schema.ResourceData, _ interface{}) error {
cfURL := fmt.Sprintf("%v/hermes/triggers/%v/%v", getCfUrl(), urlEncode(d.Get("event").(string)), d.Get("pipeline"))
return deleteCodefreshObject(cfURL)
}

func resourceCronTriggerImport(d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
return importCodefreshObject(
d,
getCronTriggerFromCodefresh,
mapCronTriggerToResource)
}

func mapResourceToCronTrigger(d *schema.ResourceData) codefreshObject {
return &cronTrigger{
Pipeline: d.Get("pipeline").(string),
Event: d.Get("event").(string),
}
}

// readCronTriggerString reads a simple string response as is returned from CREATE
func readCronTriggerString(originalObject *schema.ResourceData, _ []byte) (codefreshObject, error) {
// create doesn't return the object, so we'll just wing it and return the object we sent for creation
return mapResourceToCronTrigger(originalObject), nil
}

// readCronTrigger reads a JSON type response as is returned from GET
func readCronTrigger(_ *schema.ResourceData, b []byte) (codefreshObject, error) {
var cronTriggerResp []*cronTrigger

err := json.Unmarshal(b, &cronTriggerResp)
if err != nil {
return nil, err
}

if len(cronTriggerResp) > 1 {
return nil, fmt.Errorf("More than 1 trigger per pipeline is not supported. Found %v triggers.", len(cronTriggerResp))
}
if len(cronTriggerResp) == 0 {
return nil, nil
}
return cronTriggerResp[0], nil
}

func getCronTriggerFromCodefresh(d *schema.ResourceData) (codefreshObject, error) {
pipeline := d.Id()
// get the trigger
cfURL := fmt.Sprintf("%v/hermes/triggers/pipeline/%v", getCfUrl(), pipeline)
return getFromCodefresh(d, cfURL, readCronTrigger)
}

func mapCronTriggerToResource(cfObject codefreshObject, d *schema.ResourceData) error {
cronTrigger := cfObject.(*cronTrigger)
d.SetId(cronTrigger.getID())

err := d.Set("pipeline", cronTrigger.Pipeline)
if err != nil {
return err
}

err = d.Set("event", cronTrigger.Event)
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 7eada8d

Please sign in to comment.