func ParseResourceJson()

in internal/langserver/handlers/command/resource_json_converter.go [80:178]


func ParseResourceJson(content string) (*hclwrite.Block, error) {
	var model resourceModel
	err := json.Unmarshal([]byte(content), &model)
	if err != nil {
		return nil, fmt.Errorf("unable to unmarshal JSON content: %w", err)
	}

	block := hclwrite.NewBlock("resource", []string{})
	parentId := ""
	if armId, err := arm.ParseResourceID(model.ID); err == nil {
		// use the resource type from the ID
		model.Type = armId.ResourceType.String()
		parentId = armId.Parent.String()
	}
	if parentId == "" {
		for _, v := range model.DependsOn {
			armId, err := arm.ParseResourceID(v)
			if err != nil {
				continue
			}
			if armId.ResourceType.String() == GetParentType(model.Type) {
				parentId = armId.String()
				break
			}
		}
	}
	if parentId == "" {
		parentId = "/subscriptions/${var.subscriptionId}/resourceGroups/${var.resourceGroupName}"
	}

	apiVersions := azure.GetApiVersions(model.Type)
	apiVersion := "TODO"
	if len(apiVersions) > 0 {
		apiVersion = apiVersions[len(apiVersions)-1]
	}

	label := pluralizeClient.Singular(LastSegment(model.Type))
	block.SetLabels([]string{"azapi_resource", label})
	block.Body().SetAttributeValue("type", cty.StringVal(fmt.Sprintf("%s@%s", model.Type, apiVersion)))
	block.Body().SetAttributeValue("parent_id", cty.StringVal(parentId))

	nameValue := model.Name[strings.LastIndex(model.Name, "/")+1:]
	block.Body().SetAttributeValue("name", cty.StringVal(nameValue))

	def, _ := azure.GetResourceDefinition(model.Type, apiVersion)
	if model.Location != "" && (def == nil || canResourceHaveProperty(def, "location")) {
		block.Body().SetAttributeValue("location", cty.StringVal(model.Location))
	}

	if model.Identity != nil && !strings.EqualFold(model.Identity.Type, "None") {
		identityBlock := hclwrite.NewBlock("identity", nil)
		identityBlock.Body().SetAttributeValue("type", cty.StringVal(model.Identity.Type))
		if len(model.Identity.UserAssignedIdentities) > 0 {
			identityIds := make([]cty.Value, 0)
			for k := range model.Identity.UserAssignedIdentities {
				identityIds = append(identityIds, cty.StringVal(k))
			}
			identityBlock.Body().SetAttributeValue("identity_ids", cty.ListVal(identityIds))
		} else {
			identityBlock.Body().SetAttributeValue("identity_ids", cty.ListValEmpty(cty.String))
		}
		block.Body().AppendBlock(identityBlock)
	}

	var bodyMap map[string]interface{}
	err = json.Unmarshal([]byte(content), &bodyMap)
	if err != nil {
		return nil, fmt.Errorf("unable to unmarshal JSON content: %w", err)
	}
	delete(bodyMap, "type")
	delete(bodyMap, "location")
	delete(bodyMap, "id")
	delete(bodyMap, "tags")
	delete(bodyMap, "identity")
	bodyMap["name"] = nameValue
	if label == "basicPublishingCredentialsPolicy" {
		fmt.Println(bodyMap)
	}
	if def != nil {
		writeOnlyBody := def.GetWriteOnly(NormalizeObject(bodyMap))
		ok := false
		bodyMap, ok = writeOnlyBody.(map[string]interface{})
		if !ok {
			log.Printf("[ERROR] unable to get write only body, result: %v", writeOnlyBody)
		}
	}
	delete(bodyMap, "name")
	block.Body().SetAttributeValue("body", toCtyValue(bodyMap))

	if len(model.Tags) > 0 {
		tags := map[string]cty.Value{}
		for k, v := range model.Tags {
			tags[k] = cty.StringVal(v)
		}
		block.Body().SetAttributeValue("tags", cty.MapVal(tags))
	}

	return block, nil
}