Skip to content

Commit

Permalink
Merge pull request #1 from ajbosco/segment_config_go_v0.2.0
Browse files Browse the repository at this point in the history
use segment-config-go v0.2.0
  • Loading branch information
ajbosco authored Dec 10, 2018
2 parents 8537e3d + fc3b74b commit 3466413
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 75 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A [Terraform](https://www.terraform.io/) provider for [Segment](https://www.segm
Create and manage Segment [sources](https://segment.com/docs/sources/)
```
resource "segment_source" "test_source" {
name = "workspaces/your-workspace/sources/your-source"
source_name = "your-source"
catalog_name = "catalog/sources/javascript"
}
```
Expand All @@ -16,7 +16,7 @@ Create and manage Segment [destinations](https://segment.com/docs/destinations/)
```
resource "segment_destination" "test_destination" {
source_name = "test_source"
destination_name = "workspaces/your-workspace/sources/your-source/destinations/google-analytics"
destination_name = "google-analytics"
connection_mode = "CLOUD"
enabled = false
configs = [{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/ajbosco/terraform-provider-segment

require (
github.com/ajbosco/segment-config-go v0.1.0
github.com/ajbosco/segment-config-go v0.2.0
github.com/apparentlymart/go-cidr v1.0.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/hashicorp/go-getter v0.0.0-20181119194526-bd1edc22f8ea // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tj
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/ajbosco/segment-config-go v0.1.0 h1:FT2X+DSsBTVl+jL/WXldfvTE4QWIdqLnjmpp2Nfxp8Q=
github.com/ajbosco/segment-config-go v0.1.0/go.mod h1:f+rLFCLd9lq60T4ze+oGTMCbL4a9EHxIigR+NyhkfME=
github.com/ajbosco/segment-config-go v0.2.0 h1:qk5Z5yLkEjtysC3YvehLb4a+KvT00KA44W+u/WUVzHU=
github.com/ajbosco/segment-config-go v0.2.0/go.mod h1:f+rLFCLd9lq60T4ze+oGTMCbL4a9EHxIigR+NyhkfME=
github.com/apparentlymart/go-cidr v1.0.0 h1:lGDvXx8Lv9QHjrAVP7jyzleG4F9+FkRhJcEsDFxeb8w=
github.com/apparentlymart/go-cidr v1.0.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
Expand Down
3 changes: 0 additions & 3 deletions segment/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ func Provider() terraform.ResourceProvider {
"segment_source": resourceSegmentSource(),
"segment_destination": resourceSegmentDestination(),
},
// DataSourcesMap: map[string]*schema.Resource{
// "segment_project": dataSourceSegmentProject(),
// },
ConfigureFunc: configureFunc(),
}
}
Expand Down
41 changes: 11 additions & 30 deletions segment/resource_segment_destination.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package segment

import (
"strings"

"github.com/ajbosco/segment-config-go/segment"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -63,61 +61,43 @@ func resourceSegmentDestinationCreate(r *schema.ResourceData, meta interface{})
enabled := r.Get("enabled").(bool)
configs := r.Get("configs").(*schema.Set)

newDest := segment.Destination{
Name: destName,
Enabled: enabled,
ConnectionMode: connMode,
Configs: extractConfigs(configs),
}

dest, err := client.CreateDestination(srcName, newDest)
dest, err := client.CreateDestination(srcName, destName, connMode, enabled, extractConfigs(configs))
if err != nil {
return err
}

// the id of the destination is the last value in the name path
splitName := strings.Split(dest.Name, "/")
id := splitName[len(splitName)-1]
r.SetId(id)
r.SetId(dest.Name)

return resourceSegmentDestinationRead(r, meta)
}

func resourceSegmentDestinationRead(r *schema.ResourceData, meta interface{}) error {
client := meta.(*segment.Client)
destName := r.Id()
srcName := r.Get("source_name").(string)
id := r.Id()
destName := idToName(id)

d, err := client.GetDestination(srcName, destName)
if err != nil {
return err
}

r.Set("name", d.Name)
r.Set("connection_mod", d.ConnectionMode)
r.Set("enabled", d.Enabled)
r.Set("configs", d.Configs)
r.Set("connection_mode", d.ConnectionMode)

return nil
}

func resourceSegmentDestinationUpdate(r *schema.ResourceData, meta interface{}) error {
client := meta.(*segment.Client)
destID := r.Id()
destName := r.Get("destination_name").(string)
srcName := r.Get("source_name").(string)
configs := r.Get("configs").(*schema.Set)
enabled := r.Get("enabled").(bool)
id := r.Id()
destName := idToName(id)

dest := segment.Destination{
Name: destName,
Enabled: enabled,
Configs: extractConfigs(configs),
}
// updateMask determines which fields Segment will update
updateMask := segment.UpdateMask{Paths: []string{"destination.config", "destination.enabled"}}

_, err := client.UpdateDestination(srcName, destID, dest, updateMask)
_, err := client.UpdateDestination(srcName, destName, enabled, extractConfigs(configs))
if err != nil {
return err
}
Expand All @@ -127,10 +107,11 @@ func resourceSegmentDestinationUpdate(r *schema.ResourceData, meta interface{})

func resourceSegmentDestinationDelete(r *schema.ResourceData, meta interface{}) error {
client := meta.(*segment.Client)
destName := r.Id()
srcName := r.Get("source_name").(string)
id := r.Id()
destName := idToName(id)

err := client.DeleteDestinaton(srcName, destName)
err := client.DeleteDestination(srcName, destName)
if err != nil {
return err
}
Expand Down
37 changes: 19 additions & 18 deletions segment/resource_segment_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func resourceSegmentSource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
"source_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Expand All @@ -29,50 +29,51 @@ func resourceSegmentSource() *schema.Resource {

func resourceSegmentSourceCreate(r *schema.ResourceData, meta interface{}) error {
client := meta.(*segment.Client)
name := r.Get("name").(string)
catalogName := r.Get("catalog_name").(string)
srcName := r.Get("source_name").(string)
catName := r.Get("catalog_name").(string)

newSource := segment.Source{
Name: name,
CatalogName: catalogName,
}

source, err := client.CreateSource(newSource)
source, err := client.CreateSource(srcName, catName)
if err != nil {
return err
}

// the id of the source is the last value in the name path
splitName := strings.Split(source.Name, "/")
id := splitName[len(splitName)-1]
r.SetId(id)
r.SetId(source.Name)

return resourceSegmentSourceRead(r, meta)
}

func resourceSegmentSourceRead(r *schema.ResourceData, meta interface{}) error {
client := meta.(*segment.Client)
name := r.Id()
id := r.Id()

s, err := client.GetSource(name)
srcName := idToName(id)

s, err := client.GetSource(srcName)
if err != nil {
return err
}

r.Set("name", s.Name)
r.Set("catalog_name", s.CatalogName)

return nil
}

func resourceSegmentSourceDelete(r *schema.ResourceData, meta interface{}) error {
client := meta.(*segment.Client)
name := r.Id()
id := r.Id()

srcName := idToName(id)

err := client.DeleteSource(name)
err := client.DeleteSource(srcName)
if err != nil {
return err
}

return nil
}

func idToName(id string) string {
splitID := strings.Split(id, "/")

return splitID[len(splitID)-1]
}
42 changes: 27 additions & 15 deletions vendor/github.com/ajbosco/segment-config-go/segment/destinations.go

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

16 changes: 11 additions & 5 deletions vendor/github.com/ajbosco/segment-config-go/segment/sources.go

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# github.com/agext/levenshtein v1.2.1
github.com/agext/levenshtein
# github.com/ajbosco/segment-config-go v0.1.0
# github.com/ajbosco/segment-config-go v0.2.0
github.com/ajbosco/segment-config-go/segment
# github.com/apparentlymart/go-cidr v1.0.0
github.com/apparentlymart/go-cidr/cidr
Expand Down

0 comments on commit 3466413

Please sign in to comment.