Skip to content

Commit

Permalink
update error msg for existing deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan de Jesus committed Feb 26, 2021
1 parent fb27bcf commit 2c65c24
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
20 changes: 20 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dev.tfrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
provider_installation {
dev_overrides {
"twentyfourg/dev24g" = "/home/24g/go/bin"
}
direct {}
}
26 changes: 18 additions & 8 deletions dev24g/resource_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package dev24g
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/url"
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand All @@ -19,6 +21,7 @@ type Deployment struct {
UUID string `json:"uuid,omitempty"`
}

// Stage structure for handing stage
type Stage struct {
Name string `json:"name"`
}
Expand All @@ -29,6 +32,10 @@ func resourceDeployment() *schema.Resource {
Update: resourceDeploymentUpdate,
Read: resourceDeploymentRead,
Delete: resourceDeploymentDelete,
// TODO: implement import
// Importer: &schema.ResourceImporter{
// State: schema.ImportStatePassthrough,
// },

Schema: map[string]*schema.Schema{
"uuid": {
Expand Down Expand Up @@ -76,17 +83,20 @@ func resourceDeploymentCreate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}
req, err := client.Post(fmt.Sprintf("2.0/repositories/%s/environments/",
resp, err := client.Post(fmt.Sprintf("2.0/repositories/%s/environments/",
d.Get("repository").(string),
), bytes.NewBuffer(bytedata))

if err != nil {
if strings.Contains(err.Error(), "400") {
return errors.New("Deployment already exists")
}
return err
}

var deployment Deployment

body, readerr := ioutil.ReadAll(req.Body)
body, readerr := ioutil.ReadAll(resp.Body)
if readerr != nil {
return readerr
}
Expand All @@ -104,16 +114,16 @@ func resourceDeploymentCreate(d *schema.ResourceData, m interface{}) error {
func resourceDeploymentRead(d *schema.ResourceData, m interface{}) error {

client := m.(*Client)
req, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/environments/%s",
resp, _ := client.Get(fmt.Sprintf("2.0/repositories/%s/environments/%s",
d.Get("repository").(string),
d.Get("uuid").(string),
))

log.Printf("ID: %s", url.PathEscape(d.Id()))

if req.StatusCode == 200 {
if resp.StatusCode == 200 {
var Deployment Deployment
body, readerr := ioutil.ReadAll(req.Body)
body, readerr := ioutil.ReadAll(resp.Body)
if readerr != nil {
return readerr
}
Expand All @@ -128,7 +138,7 @@ func resourceDeploymentRead(d *schema.ResourceData, m interface{}) error {
d.Set("stage", Deployment.Stage.Name)
}

if req.StatusCode == 404 {
if resp.StatusCode == 404 {
d.SetId("")
return nil
}
Expand All @@ -144,7 +154,7 @@ func resourceDeploymentUpdate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}
req, err := client.Put(fmt.Sprintf("2.0/repositories/%s/environments/%s",
resp, err := client.Put(fmt.Sprintf("2.0/repositories/%s/environments/%s",
d.Get("repository").(string),
d.Get("uuid").(string),
), bytes.NewBuffer(bytedata))
Expand All @@ -153,7 +163,7 @@ func resourceDeploymentUpdate(d *schema.ResourceData, m interface{}) error {
return err
}

if req.StatusCode != 200 {
if resp.StatusCode != 200 {
return nil
}

Expand Down
30 changes: 30 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
terraform {
required_providers {
dev24g = {
source = "twentyfourg/dev24g"
}
}
required_version = "~>0.14.3"
}

# Configure the Provider
provider "dev24g" {
workspace = "24g"
}

data "dev24g_bitbucket_repository" "api" {
name = "2222-13-billing-tool"
}

resource "dev24g_bitbucket_deployment" "evan" {
name = "dev-api"
stage = "Test"
repository = "${data.dev24g_bitbucket_repository.api.workspace}/${data.dev24g_bitbucket_repository.api.name}"
}

resource "dev24g_bitbucket_deployment_variable" "foobar" {
key = "foo"
value = "bar"
secured = false
deployment = dev24g_bitbucket_deployment.evan.id
}

0 comments on commit 2c65c24

Please sign in to comment.