mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-15 19:22:46 -06:00
provider/consul: Simplify
This commit is contained in:
parent
e4450fcd51
commit
719217d64d
@ -36,6 +36,7 @@ func resource_consul_keys_create(
|
|||||||
// Merge the diff into the state so that we have all the attributes
|
// Merge the diff into the state so that we have all the attributes
|
||||||
// properly.
|
// properly.
|
||||||
rs := s.MergeDiff(d)
|
rs := s.MergeDiff(d)
|
||||||
|
rs.ID = "consul"
|
||||||
|
|
||||||
// Check if the datacenter should be computed
|
// Check if the datacenter should be computed
|
||||||
dc := rs.Attributes["datacenter"]
|
dc := rs.Attributes["datacenter"]
|
||||||
@ -51,30 +52,19 @@ func resource_consul_keys_create(
|
|||||||
// Get the keys
|
// Get the keys
|
||||||
keys, ok := flatmap.Expand(rs.Attributes, "key").([]interface{})
|
keys, ok := flatmap.Expand(rs.Attributes, "key").([]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
return s, fmt.Errorf("Failed to unroll keys")
|
return rs, fmt.Errorf("Failed to unroll keys")
|
||||||
}
|
}
|
||||||
|
|
||||||
kv := p.client.KV()
|
kv := p.client.KV()
|
||||||
qOpts := consulapi.QueryOptions{Datacenter: dc}
|
qOpts := consulapi.QueryOptions{Datacenter: dc}
|
||||||
wOpts := consulapi.WriteOptions{Datacenter: dc}
|
wOpts := consulapi.WriteOptions{Datacenter: dc}
|
||||||
for _, raw := range keys {
|
for idx, raw := range keys {
|
||||||
sub := raw.(map[string]interface{})
|
key, path, sub, err := parse_key(raw)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return s, fmt.Errorf("Failed to unroll: %#v", raw)
|
return rs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
key, ok := sub["name"].(string)
|
if valueRaw, shouldSet := sub["value"]; shouldSet {
|
||||||
if !ok {
|
|
||||||
return s, fmt.Errorf("Failed to expand key '%#v'", sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
path, ok := sub["path"].(string)
|
|
||||||
if !ok {
|
|
||||||
return s, fmt.Errorf("Failed to get path for key '%s'", key)
|
|
||||||
}
|
|
||||||
|
|
||||||
valueRaw, shouldSet := sub["value"]
|
|
||||||
if shouldSet {
|
|
||||||
value, ok := valueRaw.(string)
|
value, ok := valueRaw.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return rs, fmt.Errorf("Failed to get value for key '%s'", key)
|
return rs, fmt.Errorf("Failed to get value for key '%s'", key)
|
||||||
@ -86,39 +76,17 @@ func resource_consul_keys_create(
|
|||||||
return rs, fmt.Errorf("Failed to set Consul key '%s': %v", path, err)
|
return rs, fmt.Errorf("Failed to set Consul key '%s': %v", path, err)
|
||||||
}
|
}
|
||||||
rs.Attributes[fmt.Sprintf("var.%s", key)] = value
|
rs.Attributes[fmt.Sprintf("var.%s", key)] = value
|
||||||
|
rs.Attributes[fmt.Sprintf("key.%d.value", idx)] = value
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("[DEBUG] Getting key '%s' in %s", path, dc)
|
log.Printf("[DEBUG] Getting key '%s' in %s", path, dc)
|
||||||
pair, _, err := kv.Get(path, &qOpts)
|
pair, _, err := kv.Get(path, &qOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rs, fmt.Errorf("Failed to get Consul key '%s': %v", path, err)
|
return rs, fmt.Errorf("Failed to get Consul key '%s': %v", path, err)
|
||||||
}
|
}
|
||||||
|
rs.Attributes[fmt.Sprintf("var.%s", key)] = attribute_value(sub, key, pair)
|
||||||
// Check for a default value
|
|
||||||
var defaultVal string
|
|
||||||
setDefault := false
|
|
||||||
if raw, ok := sub["default"]; ok {
|
|
||||||
switch def := raw.(type) {
|
|
||||||
case string:
|
|
||||||
setDefault = true
|
|
||||||
defaultVal = def
|
|
||||||
case bool:
|
|
||||||
setDefault = true
|
|
||||||
defaultVal = strconv.FormatBool(def)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if pair == nil && setDefault {
|
|
||||||
rs.Attributes[fmt.Sprintf("var.%s", key)] = defaultVal
|
|
||||||
} else if pair == nil {
|
|
||||||
rs.Attributes[fmt.Sprintf("var.%s", key)] = ""
|
|
||||||
} else {
|
|
||||||
rs.Attributes[fmt.Sprintf("var.%s", key)] = string(pair.Value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set an ID
|
|
||||||
rs.ID = "consul"
|
|
||||||
return rs, nil
|
return rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,9 +106,9 @@ func resource_consul_keys_destroy(
|
|||||||
dc := s.Attributes["datacenter"]
|
dc := s.Attributes["datacenter"]
|
||||||
wOpts := consulapi.WriteOptions{Datacenter: dc}
|
wOpts := consulapi.WriteOptions{Datacenter: dc}
|
||||||
for _, raw := range keys {
|
for _, raw := range keys {
|
||||||
sub := raw.(map[string]interface{})
|
_, path, sub, err := parse_key(raw)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to unroll: %#v", raw)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore if the key is non-managed
|
// Ignore if the key is non-managed
|
||||||
@ -149,19 +117,8 @@ func resource_consul_keys_destroy(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
key, ok := sub["name"].(string)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Failed to expand key '%#v'", sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
path, ok := sub["path"].(string)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Failed to get path for key '%s'", key)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("[DEBUG] Deleting key '%s' in %s", path, dc)
|
log.Printf("[DEBUG] Deleting key '%s' in %s", path, dc)
|
||||||
_, err := kv.Delete(path, &wOpts)
|
if _, err := kv.Delete(path, &wOpts); err != nil {
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to delete Consul key '%s': %v", path, err)
|
return fmt.Errorf("Failed to delete Consul key '%s': %v", path, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -181,26 +138,18 @@ func resource_consul_keys_diff(
|
|||||||
c *terraform.ResourceConfig,
|
c *terraform.ResourceConfig,
|
||||||
meta interface{}) (*terraform.ResourceDiff, error) {
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
||||||
|
|
||||||
// Get the list of keys
|
// Determine the list of computed variables
|
||||||
var computed []string
|
var computed []string
|
||||||
keys, ok := flatmap.Expand(flatmap.Flatten(c.Config), "key").([]interface{})
|
keys, ok := flatmap.Expand(flatmap.Flatten(c.Config), "key").([]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
goto AFTER
|
goto AFTER
|
||||||
}
|
}
|
||||||
for _, sub := range keys {
|
for _, sub := range keys {
|
||||||
subMap, ok := sub.(map[string]interface{})
|
key, _, _, err := parse_key(sub)
|
||||||
if !ok {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
nameRaw, ok := subMap["name"]
|
computed = append(computed, "var."+key)
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
name, ok := nameRaw.(string)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
computed = append(computed, "var."+name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AFTER:
|
AFTER:
|
||||||
@ -230,20 +179,10 @@ func resource_consul_keys_refresh(
|
|||||||
// Update each key
|
// Update each key
|
||||||
dc := s.Attributes["datacenter"]
|
dc := s.Attributes["datacenter"]
|
||||||
opts := consulapi.QueryOptions{Datacenter: dc}
|
opts := consulapi.QueryOptions{Datacenter: dc}
|
||||||
for _, raw := range keys {
|
for idx, raw := range keys {
|
||||||
sub := raw.(map[string]interface{})
|
key, path, sub, err := parse_key(raw)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return s, fmt.Errorf("Failed to unroll: %#v", raw)
|
return s, err
|
||||||
}
|
|
||||||
|
|
||||||
key, ok := sub["name"].(string)
|
|
||||||
if !ok {
|
|
||||||
return s, fmt.Errorf("Failed to expand key '%#v'", sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
path, ok := sub["path"].(string)
|
|
||||||
if !ok {
|
|
||||||
return s, fmt.Errorf("Failed to get path for key '%s'", key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] Refreshing value of key '%s' in %s", path, dc)
|
log.Printf("[DEBUG] Refreshing value of key '%s' in %s", path, dc)
|
||||||
@ -252,31 +191,55 @@ func resource_consul_keys_refresh(
|
|||||||
return s, fmt.Errorf("Failed to get value for path '%s' from Consul: %v", path, err)
|
return s, fmt.Errorf("Failed to get value for path '%s' from Consul: %v", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for a default value
|
setVal := attribute_value(sub, key, pair)
|
||||||
var defaultVal string
|
s.Attributes[fmt.Sprintf("var.%s", key)] = setVal
|
||||||
setDefault := false
|
if _, ok := sub["value"]; ok {
|
||||||
if raw, ok := sub["default"]; ok {
|
s.Attributes[fmt.Sprintf("key.%d.value", idx)] = setVal
|
||||||
switch def := raw.(type) {
|
|
||||||
case string:
|
|
||||||
setDefault = true
|
|
||||||
defaultVal = def
|
|
||||||
case bool:
|
|
||||||
setDefault = true
|
|
||||||
defaultVal = strconv.FormatBool(def)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if pair == nil && setDefault {
|
|
||||||
s.Attributes[fmt.Sprintf("var.%s", key)] = defaultVal
|
|
||||||
} else if pair == nil {
|
|
||||||
s.Attributes[fmt.Sprintf("var.%s", key)] = ""
|
|
||||||
} else {
|
|
||||||
s.Attributes[fmt.Sprintf("var.%s", key)] = string(pair.Value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse_key is used to parse a key into a name, path, config or error
|
||||||
|
func parse_key(raw interface{}) (string, string, map[string]interface{}, error) {
|
||||||
|
sub, ok := raw.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return "", "", nil, fmt.Errorf("Failed to unroll: %#v", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
key, ok := sub["name"].(string)
|
||||||
|
if !ok {
|
||||||
|
return "", "", nil, fmt.Errorf("Failed to expand key '%#v'", sub)
|
||||||
|
}
|
||||||
|
|
||||||
|
path, ok := sub["path"].(string)
|
||||||
|
if !ok {
|
||||||
|
return "", "", nil, fmt.Errorf("Failed to get path for key '%s'", key)
|
||||||
|
}
|
||||||
|
return key, path, sub, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// attribute_value determienes the value for a key
|
||||||
|
func attribute_value(sub map[string]interface{}, key string, pair *consulapi.KVPair) string {
|
||||||
|
// Use the value if given
|
||||||
|
if pair != nil {
|
||||||
|
return string(pair.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use a default if given
|
||||||
|
if raw, ok := sub["default"]; ok {
|
||||||
|
switch def := raw.(type) {
|
||||||
|
case string:
|
||||||
|
return def
|
||||||
|
case bool:
|
||||||
|
return strconv.FormatBool(def)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No value
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// get_dc is used to get the datacenter of the local agent
|
// get_dc is used to get the datacenter of the local agent
|
||||||
func get_dc(client *consulapi.Client) (string, error) {
|
func get_dc(client *consulapi.Client) (string, error) {
|
||||||
info, err := client.Agent().Self()
|
info, err := client.Agent().Self()
|
||||||
|
Loading…
Reference in New Issue
Block a user