func()

in types/azapi_resource.go [59:80]


func (r *AzapiResource) importBlock() *hclwrite.Block {
	importBlock := hclwrite.NewBlock("import", nil)
	if r.IsMultipleResources() {
		forEachMap := make(map[string]cty.Value)
		for _, instance := range r.Instances {
			switch v := instance.Index.(type) {
			case string:
				forEachMap[instance.ResourceId] = cty.StringVal(v)
			default:
				value, _ := strconv.ParseInt(fmt.Sprintf("%v", v), 10, 64)
				forEachMap[instance.ResourceId] = cty.NumberIntVal(value)
			}
		}
		importBlock.Body().SetAttributeValue("for_each", cty.MapVal(forEachMap))
		importBlock.Body().SetAttributeTraversal("id", hcl.Traversal{hcl.TraverseRoot{Name: "each"}, hcl.TraverseAttr{Name: "key"}})
		importBlock.Body().SetAttributeTraversal("to", hcl.Traversal{hcl.TraverseRoot{Name: r.ResourceType}, hcl.TraverseAttr{Name: fmt.Sprintf("%s[each.value]", r.Label)}})
	} else {
		importBlock.Body().SetAttributeValue("id", cty.StringVal(r.Instances[0].ResourceId))
		importBlock.Body().SetAttributeTraversal("to", hcl.Traversal{hcl.TraverseRoot{Name: r.ResourceType}, hcl.TraverseAttr{Name: r.Label}})
	}
	return importBlock
}