From 3449fd0909c635eb5c2c684f9cd11243fe5ca3fa Mon Sep 17 00:00:00 2001 From: Adam Boscarino Date: Thu, 13 Dec 2018 10:30:17 -0500 Subject: [PATCH] add importers for destinations and sources --- go.mod | 1 + go.sum | 13 +++++++-- segment/resource_segment_destination.go | 37 +++++++++++++++++++++++++ segment/resource_segment_source.go | 20 +++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 3d8839f..4db0942 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( 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/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect github.com/hashicorp/go-getter v0.0.0-20181119194526-bd1edc22f8ea // indirect github.com/hashicorp/go-hclog v0.0.0-20181001195459-61d530d6c27f // indirect github.com/hashicorp/go-plugin v0.0.0-20181030172320-54b6ff97d818 // indirect diff --git a/go.sum b/go.sum index 4394cf2..eb7cc1f 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,10 @@ github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8= 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 h1:ZSTrOEhiM5J5RFxEaFvMZVEAM1KvT1YzbEOwB2EAGjA= github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0= github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk= @@ -25,7 +24,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-test/deep v1.0.1 h1:UQhStjbkDClarlmv0am7OXXO4/GaPdCGiUiMTvi28sg= github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -68,9 +70,12 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -100,11 +105,14 @@ github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1 h1:ccV59UEOTzVDnDUEFdT95ZzHVZ+5+158q8+SJb2QV5w= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok= github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= @@ -122,6 +130,7 @@ golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20181207154023-610586996380 h1:zPQexyRtNYBc7bcHmehl1dH6TB3qn8zytv8cBGLDNY0= golang.org/x/net v0.0.0-20181207154023-610586996380/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= diff --git a/segment/resource_segment_destination.go b/segment/resource_segment_destination.go index 9a4d270..b4f3d4c 100644 --- a/segment/resource_segment_destination.go +++ b/segment/resource_segment_destination.go @@ -1,6 +1,9 @@ package segment import ( + "fmt" + "strings" + "github.com/ajbosco/segment-config-go/segment" "github.com/hashicorp/terraform/helper/schema" ) @@ -50,6 +53,9 @@ func resourceSegmentDestination() *schema.Resource { Read: resourceSegmentDestinationRead, Update: resourceSegmentDestinationUpdate, Delete: resourceSegmentDestinationDelete, + Importer: &schema.ResourceImporter{ + State: resourceSegmentDestinationImport, + }, } } @@ -119,6 +125,37 @@ func resourceSegmentDestinationDelete(r *schema.ResourceData, meta interface{}) return nil } +func resourceSegmentDestinationImport(r *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + client := meta.(*segment.Client) + s := strings.SplitN(r.Id(), "/", 2) + if len(s) != 2 { + return nil, fmt.Errorf( + "invalid destination import format: %s (expected /)", + r.Id(), + ) + } + + srcName := s[0] + destName := s[1] + + d, err := client.GetDestination(srcName, destName) + if err != nil { + return nil, fmt.Errorf("invalid destination: %q; err: %v", r.Id(), err) + } + + r.SetId(d.Name) + r.Set("source_name", srcName) + r.Set("destination_name", destName) + r.Set("enabled", d.Enabled) + r.Set("configs", d.Configs) + r.Set("connection_mode", d.ConnectionMode) + + results := make([]*schema.ResourceData, 1) + results[0] = r + + return results, nil +} + func extractConfigs(s *schema.Set) []segment.DestinationConfig { configs := []segment.DestinationConfig{} diff --git a/segment/resource_segment_source.go b/segment/resource_segment_source.go index 569caec..472d5ff 100644 --- a/segment/resource_segment_source.go +++ b/segment/resource_segment_source.go @@ -1,6 +1,7 @@ package segment import ( + "fmt" "strings" "github.com/ajbosco/segment-config-go/segment" @@ -24,6 +25,9 @@ func resourceSegmentSource() *schema.Resource { Create: resourceSegmentSourceCreate, Read: resourceSegmentSourceRead, Delete: resourceSegmentSourceDelete, + Importer: &schema.ResourceImporter{ + State: resourceSegmentSourceImport, + }, } } @@ -72,6 +76,22 @@ func resourceSegmentSourceDelete(r *schema.ResourceData, meta interface{}) error return nil } +func resourceSegmentSourceImport(r *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + client := meta.(*segment.Client) + s, err := client.GetSource(r.Id()) + if err != nil { + return nil, fmt.Errorf("invalid source: %q; err: %v", r.Id(), err) + } + + r.SetId(s.Name) + r.Set("catalog_name", s.CatalogName) + + results := make([]*schema.ResourceData, 1) + results[0] = r + + return results, nil +} + func idToName(id string) string { splitID := strings.Split(id, "/")