netascode / terraform-provider-fmc

Mozilla Public License 2.0
1 stars 3 forks source link

Multiline attribute values issue #39

Open shebang42 opened 3 weeks ago

shebang42 commented 3 weeks ago

Hello,

There is the "old style" provider at https://registry.terraform.io/providers/CiscoDevNet/fmc . However, this does not support all the features we require. Expecially all the Chassis API endpoints are missing. Extending this new provider seems much easier, but I can use some guidance here.

This provider has YAML generator files, similar to the ASR provider. When implementing the features we require, I run into issues with multi-line parameters/examples. I have tried multiple ways of ways of constructing the example attribute values, but nothing seems to work.

Here is the endpoint to be added:

---
name: LogicalDevice
rest_endpoint: /api/fmc_config/v1/domain/{DOMAIN_UUID}/chassis/fmcmanagedchassis/{CONTAINER_UUID}/logicaldevices/{OBJECT_ID}
data_source_name_query: true
doc_category: Chassis
attributes:
  - model_name: name
    type: String
    mandatory: true
    description: The name of the logical device.
    example: ftd1
  - model_name: managementBootstrap
    type: Object
    description: Management bootstrap configuration for the logical device.

The above YAML code is not complete. Example needs to be added as well, and this is where the issue is.

This is a snippet of the JSON that needs to be generated:

<..>
            "managementBootstrap": {
                "ipv4": {
                    "gateway": "10.1.1.2",
                    "mask": "255.255.255.0",
                    "ip": "10.1.1.1"
                },
                "ipv6": {},
                "permitExpertMode": "yes",
                "firewallMode": "routed",
                "adminPassword": "********"
            },

And now the "example" part of the generator file.

example with \n\n for newlines

This is similar to https://github.com/CiscoDevNet/terraform-provider-iosxr/blob/main/gen/definitions/as_path_set.yaml#L11

<..>
  - model_name: managementBootstrap
    type: Object
    description: Management bootstrap configuration for the logical device.
    example: "ipv4:\\n  gateway: 192.168.42.1\\n  ip: 192.168.42.42\\n  mask: 255.255.255.0\\n"
> go generate
<..>
examples/resources/fmc_network/resource.tf
╷
│ Error: Missing newline after argument
│ 
│   on examples/resources/fmc_logicaldevice/resource.tf line 3, in resource "fmc_logicaldevice" "example":
│    3:   management_bootstrap = ipv4:\n  gateway: 192.168.42.1\n  ip: 192.168.42.42\n  mask: 255.255.255.0\n
│ 
│ An argument definition must end with a newline.
╵

╷
│ Error: Invalid character
│ 
│   on examples/resources/fmc_logicaldevice/resource.tf line 3, in resource "fmc_logicaldevice" "example":
│    3:   management_bootstrap = ipv4:\n  gateway: 192.168.42.1\n  ip: 192.168.42.42\n  mask: 255.255.255.0\n
│ 
│ This character is not used within the language.
<.. more invalid chars ...>

The issues seems that the double quotes in the value of "management_bootstrap" are missing. But I am not sure if the \n\n for newlines is the correct format in the first place.

> cat resource.tf
resource "fmc_logicaldevice" "example" {
  name = "ftd1"
  management_bootstrap = ipv4:\n  gateway: 192.168.42.1\n  ip: 192.168.42.42\n  mask: 255.255.255.0\n
}

example in YAML:

    example:
      ipv4:
        gateway: 192.168.3.68
        ip: 192.168.3.78
        mask: 255.255.255.0
      adminState: enabled
      firepowerManagerIP: 192.168.1.32
      permitExpertMode: yes
      searchDomain: example.com
      firewallMode: Routed
      dnsServers: 192.168.0.123
      natId: nat1
      registrationKey: abcd1234
      adminPassword: securePassword
      fqdn: ftd1.example.com
> go generate
2024/06/10 11:00:54 Error parsing yaml: yaml: unmarshal errors:
  line 18: cannot unmarshal !!map into string
exit status 1
main.go:31: running "go": exit status 1

Can you provide an example how multiline attribute values should look like?

danischm commented 2 weeks ago

First of all, the schema here (https://github.com/netascode/terraform-provider-fmc/blob/main/gen/schema/schema.yaml) might provide some additional hints in terms of what each definition attribute is being used for.

If I understand you correctly, you want to access/expose a nested attribute in the API payload, which would work like this:

attributes:
  - model_name: gateway
    data_path: [managementBootstrap, ipv4]
    type: String
    example: 10.1.1.2
  - model_name: mask
    data_path: [managementBootstrap, ipv4]
    type: String
    example: 255.255.255.0