terraform-provider-docker/internal/provider/helpers.go
Manuel Vogel 2845519dce
fix/move helpers (#170)
* chore: rename structures file

* fix: create volume migrators

* fix: move service migrators

* fix: move network migrators

* fix: rename container migrators

* chore: move label migrators and helpers

* chore: move container structures

* chore: move network structures

* fix: move container extrahosts flattener

* fix: move container ulimits flattener

* fix: move container devices flattener

* chore: move service mappers to structures file

* chore: move image helper funcs

* chore: add constants for network refresher funcs

* chore: move plugin crud funcs to the top

* chore: move registry image funcs to the top

* chore: add resfresh func constants for volume

* chore: extract ipam config flatten func
2021-04-19 22:33:13 +09:00

124 lines
3.1 KiB
Go

package provider
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func labelToPair(label map[string]interface{}) (string, string) {
return label["label"].(string), label["value"].(string)
}
func labelSetToMap(labels *schema.Set) map[string]string {
labelsSlice := labels.List()
mapped := make(map[string]string, len(labelsSlice))
for _, label := range labelsSlice {
l, v := labelToPair(label.(map[string]interface{}))
mapped[l] = v
}
return mapped
}
func hashLabel(v interface{}) int {
labelMap := v.(map[string]interface{})
return hashStringLabel(labelMap["label"].(string))
}
func hashStringLabel(str string) int {
return schema.HashString(str)
}
func mapStringInterfaceToLabelList(labels map[string]interface{}) []interface{} {
var mapped []interface{}
for k, v := range labels {
mapped = append(mapped, map[string]interface{}{
"label": k,
"value": fmt.Sprintf("%v", v),
})
}
return mapped
}
func mapToLabelSet(labels map[string]string) *schema.Set {
var mapped []interface{}
for k, v := range labels {
mapped = append(mapped, map[string]interface{}{
"label": k,
"value": v,
})
}
return schema.NewSet(hashLabel, mapped)
}
var labelSchema = &schema.Resource{
Schema: map[string]*schema.Schema{
"label": {
Type: schema.TypeString,
Description: "Name of the label",
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Description: "Value of the label",
Required: true,
ForceNew: true,
},
},
}
// gatherImmediateSubkeys given an incomplete attribute identifier, find all
// the strings (if any) that appear after this one in the various dot-separated
// identifiers.
func gatherImmediateSubkeys(attrs map[string]string, partialKey string) []string {
immediateSubkeys := []string{}
for k := range attrs {
prefix := partialKey + "."
if strings.HasPrefix(k, prefix) {
rest := strings.TrimPrefix(k, prefix)
parts := strings.SplitN(rest, ".", 2)
immediateSubkeys = append(immediateSubkeys, parts[0])
}
}
return immediateSubkeys
}
func getLabelMapForPartialKey(attrs map[string]string, partialKey string) map[string]string {
setIDs := gatherImmediateSubkeys(attrs, partialKey)
labelMap := map[string]string{}
for _, id := range setIDs {
if id == "#" {
continue
}
prefix := partialKey + "." + id
labelMap[attrs[prefix+".label"]] = attrs[prefix+".value"]
}
return labelMap
}
func testCheckLabelMap(name string, partialKey string, expectedLabels map[string]string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attrs := s.RootModule().Resources[name].Primary.Attributes
labelMap := getLabelMapForPartialKey(attrs, partialKey)
if len(labelMap) != len(expectedLabels) {
return fmt.Errorf("expected %v labels, found %v", len(expectedLabels), len(labelMap))
}
for l, v := range expectedLabels {
if labelMap[l] != v {
return fmt.Errorf("expected value %v for label %v, got %v", v, l, labelMap[v])
}
}
return nil
}
}