Skip to content

Commit

Permalink
Begin to add tag support with default tags in provider config
Browse files Browse the repository at this point in the history
  • Loading branch information
scastrianni committed Oct 27, 2024
1 parent 7c67172 commit 971bb93
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 47 deletions.
24 changes: 13 additions & 11 deletions konnect/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ type EntityId struct {
}

type Client struct {
pat string
region string
numRetries int
retryDelay int
httpClient *http.Client
pat string
region string
numRetries int
retryDelay int
DefaultTags []string
httpClient *http.Client
}

func NewClient(pat string, region string, numRetries int, retryDelay int) (*Client, error) {
func NewClient(pat string, region string, numRetries int, retryDelay int, defaultTags []string) (*Client, error) {
c := &Client{
pat: pat,
region: region,
numRetries: numRetries,
retryDelay: retryDelay,
httpClient: &http.Client{},
pat: pat,
region: region,
numRetries: numRetries,
retryDelay: retryDelay,
DefaultTags: defaultTags,
httpClient: &http.Client{},
}
return c, nil
}
Expand Down
2 changes: 2 additions & 0 deletions konnect/client/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ func ConsumerDecodeId(s string) (string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/consumer_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ func ConsumerACLDecodeId(s string) (string, string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1], tokens[2]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/consumer_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ func ConsumerBasicDecodeId(s string) (string, string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1], tokens[2]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/consumer_hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ func ConsumerHMACDecodeId(s string) (string, string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1], tokens[2]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/consumer_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ func ConsumerJWTDecodeId(s string) (string, string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1], tokens[2]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/consumer_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ func ConsumerKeyDecodeId(s string) (string, string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1], tokens[2]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ type ControlPlaneConfig struct {
type ControlPlaneCollection struct {
ControlPlanes []ControlPlane `json:"data"`
}

//LABELS
2 changes: 2 additions & 0 deletions konnect/client/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ func PluginDecodeId(s string) (string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ func RouteDecodeId(s string) (string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1]
}

//TAGS
28 changes: 16 additions & 12 deletions konnect/client/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ const (
)

type Service struct {
ControlPlaneId string `json:"-"`
Id string `json:"id"`
Name string `json:"name"`
Retries int `json:"retries"`
Protocol string `json:"protocol"`
Host string `json:"host"`
Port int `json:"port"`
Path string `json:"path"`
ConnectTimeout int `json:"connect_timeout"`
ReadTimeout int `json:"read_timeout"`
WriteTimeout int `json:"write_timeout"`
Enabled bool `json:"enabled"`
ControlPlaneId string `json:"-"`
Id string `json:"id"`
Name string `json:"name"`
Retries int `json:"retries"`
Protocol string `json:"protocol"`
Host string `json:"host"`
Port int `json:"port"`
Path string `json:"path"`
ConnectTimeout int `json:"connect_timeout"`
ReadTimeout int `json:"read_timeout"`
WriteTimeout int `json:"write_timeout"`
Enabled bool `json:"enabled"`
AllTags []string `json:"tags"`
Tags []string `json:"-"`
}
type ServiceCollection struct {
Services []Service `json:"data"`
Expand All @@ -33,3 +35,5 @@ func ServiceDecodeId(s string) (string, string) {
tokens := strings.Split(s, IdSeparator)
return tokens[0], tokens[1]
}

//TAGS
2 changes: 2 additions & 0 deletions konnect/client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ type Team struct {
type TeamCollection struct {
Teams []Team `json:"data"`
}

//LABELS
14 changes: 13 additions & 1 deletion konnect/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func Provider() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KONNECT_RETRY_DELAY", 30),
},
"default_tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
ResourcesMap: map[string]*schema.Resource{
"konnect_control_plane": resourceControlPlane(),
Expand Down Expand Up @@ -73,9 +80,14 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
region := d.Get("region").(string)
numRetries := d.Get("num_retries").(int)
retryDelay := d.Get("retry_delay").(int)
defaultTags := []string{}
defaultTagsSet, ok := d.GetOk("default_tags")
if ok {
defaultTags = convertSetToArray(defaultTagsSet.(*schema.Set))
}

var diags diag.Diagnostics
c, err := client.NewClient(pat, region, numRetries, retryDelay)
c, err := client.NewClient(pat, region, numRetries, retryDelay, defaultTags)
if err != nil {
return nil, diag.FromErr(err)
}
Expand Down
2 changes: 0 additions & 2 deletions konnect/resource_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ func resourceConsumerUpdate(ctx context.Context, d *schema.ResourceData, m inter
buf := bytes.Buffer{}
upConsumer := client.Consumer{}
fillConsumer(&upConsumer, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
err := json.NewEncoder(&buf).Encode(upConsumer)
if err != nil {
return diag.FromErr(err)
Expand Down
2 changes: 0 additions & 2 deletions konnect/resource_consumer_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ func resourceConsumerACLUpdate(ctx context.Context, d *schema.ResourceData, m in
buf := bytes.Buffer{}
upConsumerACL := client.ConsumerACL{}
fillConsumerACL(&upConsumerACL, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
err := json.NewEncoder(&buf).Encode(upConsumerACL)
if err != nil {
return diag.FromErr(err)
Expand Down
2 changes: 0 additions & 2 deletions konnect/resource_consumer_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ func resourceConsumerBasicUpdate(ctx context.Context, d *schema.ResourceData, m
buf := bytes.Buffer{}
upConsumerBasic := client.ConsumerBasic{}
fillConsumerBasic(&upConsumerBasic, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
err := json.NewEncoder(&buf).Encode(upConsumerBasic)
if err != nil {
return diag.FromErr(err)
Expand Down
2 changes: 0 additions & 2 deletions konnect/resource_consumer_hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ func resourceConsumerHMACUpdate(ctx context.Context, d *schema.ResourceData, m i
buf := bytes.Buffer{}
upConsumerHMAC := client.ConsumerHMAC{}
fillConsumerHMAC(&upConsumerHMAC, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
err := json.NewEncoder(&buf).Encode(upConsumerHMAC)
if err != nil {
return diag.FromErr(err)
Expand Down
2 changes: 0 additions & 2 deletions konnect/resource_consumer_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ func resourceConsumerJWTUpdate(ctx context.Context, d *schema.ResourceData, m in
buf := bytes.Buffer{}
upConsumerJWT := client.ConsumerJWT{}
fillConsumerJWT(&upConsumerJWT, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
err := json.NewEncoder(&buf).Encode(upConsumerJWT)
if err != nil {
return diag.FromErr(err)
Expand Down
2 changes: 0 additions & 2 deletions konnect/resource_consumer_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ func resourceConsumerKeyUpdate(ctx context.Context, d *schema.ResourceData, m in
buf := bytes.Buffer{}
upConsumerKey := client.ConsumerKey{}
fillConsumerKey(&upConsumerKey, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
err := json.NewEncoder(&buf).Encode(upConsumerKey)
if err != nil {
return diag.FromErr(err)
Expand Down
2 changes: 0 additions & 2 deletions konnect/resource_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ func resourceRouteUpdate(ctx context.Context, d *schema.ResourceData, m interfac
buf := bytes.Buffer{}
upRoute := client.Route{}
fillRoute(&upRoute, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
err := json.NewEncoder(&buf).Encode(upRoute)
if err != nil {
return diag.FromErr(err)
Expand Down
52 changes: 43 additions & 9 deletions konnect/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func resourceService() *schema.Resource {
ReadContext: resourceServiceRead,
UpdateContext: resourceServiceUpdate,
DeleteContext: resourceServiceDelete,
CustomizeDiff: resourceDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -80,6 +81,20 @@ func resourceService() *schema.Resource {
Optional: true,
Default: true,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"all_tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"service_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -88,7 +103,19 @@ func resourceService() *schema.Resource {
}
}

func fillService(c *client.Service, d *schema.ResourceData) {
func resourceDiff(ctx context.Context, diff *schema.ResourceDiff, m interface{}) error {
c := m.(*client.Client)
tags := []string{}
tagsSet, ok := diff.GetOk("tags")
if ok {
tags = convertSetToArray(tagsSet.(*schema.Set))
}
allTags := unionArrays(tags, c.DefaultTags)
diff.SetNew("all_tags", allTags)
return nil
}

func fillService(c *client.Service, d *schema.ResourceData, defaultTags []string) {
c.ControlPlaneId = d.Get("control_plane_id").(string)
c.Host = d.Get("host").(string)
c.Enabled = d.Get("enabled").(bool)
Expand Down Expand Up @@ -124,9 +151,16 @@ func fillService(c *client.Service, d *schema.ResourceData) {
if ok {
c.WriteTimeout = writeTimeout.(int)
}
tags := []string{}
tagsSet, ok := d.GetOk("tags")
if ok {
tags = convertSetToArray(tagsSet.(*schema.Set))
c.Tags = tags
}
c.AllTags = unionArrays(tags, defaultTags)
}

func fillResourceDataFromService(c *client.Service, d *schema.ResourceData) {
func fillResourceDataFromService(c *client.Service, d *schema.ResourceData, defaultTags []string) {
d.Set("control_plane_id", c.ControlPlaneId)
d.Set("host", c.Host)
d.Set("name", c.Name)
Expand All @@ -138,6 +172,8 @@ func fillResourceDataFromService(c *client.Service, d *schema.ResourceData) {
d.Set("read_timeout", c.ReadTimeout)
d.Set("write_timeout", c.WriteTimeout)
d.Set("enabled", c.Enabled)
d.Set("all_tags", c.AllTags)
d.Set("tags", subtractArrays(c.AllTags, defaultTags))
d.Set("service_id", c.Id)
}

Expand All @@ -146,7 +182,7 @@ func resourceServiceCreate(ctx context.Context, d *schema.ResourceData, m interf
c := m.(*client.Client)
buf := bytes.Buffer{}
newService := client.Service{}
fillService(&newService, d)
fillService(&newService, d, c.DefaultTags)
err := json.NewEncoder(&buf).Encode(newService)
if err != nil {
d.SetId("")
Expand All @@ -169,7 +205,7 @@ func resourceServiceCreate(ctx context.Context, d *schema.ResourceData, m interf
}
retVal.ControlPlaneId = newService.ControlPlaneId
d.SetId(retVal.ServiceEncodeId())
fillResourceDataFromService(retVal, d)
fillResourceDataFromService(retVal, d, c.DefaultTags)
return diags
}

Expand All @@ -194,7 +230,7 @@ func resourceServiceRead(ctx context.Context, d *schema.ResourceData, m interfac
return diag.FromErr(err)
}
retVal.ControlPlaneId = controlPlaneId
fillResourceDataFromService(retVal, d)
fillResourceDataFromService(retVal, d, c.DefaultTags)
return diags
}

Expand All @@ -204,9 +240,7 @@ func resourceServiceUpdate(ctx context.Context, d *schema.ResourceData, m interf
c := m.(*client.Client)
buf := bytes.Buffer{}
upService := client.Service{}
fillService(&upService, d)
// Hide non-updateable fields
//upTeam.IsPredefined = false
fillService(&upService, d, c.DefaultTags)
err := json.NewEncoder(&buf).Encode(upService)
if err != nil {
return diag.FromErr(err)
Expand All @@ -225,7 +259,7 @@ func resourceServiceUpdate(ctx context.Context, d *schema.ResourceData, m interf
return diag.FromErr(err)
}
retVal.ControlPlaneId = controlPlaneId
fillResourceDataFromService(retVal, d)
fillResourceDataFromService(retVal, d, c.DefaultTags)
return diags
}

Expand Down
32 changes: 32 additions & 0 deletions konnect/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ func convertSetToArray(set *schema.Set) []string {
return retVal
}

func unionArrays(l1 []string, l2 []string) []string {
uniqueMap := make(map[string]bool)
retVal := []string{}
for _, item := range l1 {
if !uniqueMap[item] {
uniqueMap[item] = true
retVal = append(retVal, item)
}
}
for _, item := range l2 {
if !uniqueMap[item] {
uniqueMap[item] = true
retVal = append(retVal, item)
}
}
return retVal
}

func subtractArrays(l1 []string, l2 []string) []string {
removeMap := make(map[string]bool)
for _, item := range l2 {
removeMap[item] = true
}
retVal := []string{}
for _, item := range l1 {
if !removeMap[item] {
retVal = append(retVal, item)
}
}
return retVal
}

func find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
Expand Down

0 comments on commit 971bb93

Please sign in to comment.