netbox-community / go-netbox

The official Go API client for Netbox IPAM and DCIM service.
Other
194 stars 144 forks source link

Go client stubs sending too many parameters #90

Closed caboteria closed 1 year ago

caboteria commented 4 years ago

Hi Folks,

I'd like to change just the status of a specific IP address but am having trouble with the golang client stubs sending too many address parameters. My code looks like this:

update := &models.WritableIPAddress{Status: "active"}
fmt.Println(*update)
p := ipam.NewIpamIPAddressesPartialUpdateParams()
p.ID = first.ID
p.Data = update

_, err = c.Ipam.IpamIPAddressesPartialUpdate(p, nil)

but what goes over the wire looks like:

{"address":null,"created":"0001-01-01","last_updated":"0001-01-01T00:00:00.000Z","nat_outside":null,"status":"active","tags":null}

This causes a 400 response.

I looked at https://pkg.go.dev/github.com/netbox-community/go-netbox/netbox/models?tab=doc#WritableIPAddress and the extra fields that I'm getting in the json don't have "omitempty" in their declaration. Should I be using something different than models.WritableIPAddress so that only the parameters that I set get serialized or am I doing something tragically wrong?

Thanks!

jeremyforan commented 2 years ago

I am experiencing the same issue. It is unclear to me how to make a change to a single field of an existing Ip Address.

Can someone please provide some guidance?

Kind Regards,

jeremyforan commented 2 years ago

Here is what I have so far, but let me warn you.... it's not pretty and I suspect there is a better way.

func (n *Netbox) UpdateIpAddress(ipAddress *models.IPAddress) error {
    updates := IpToWriteable(ipAddress)

    params := ipam.NewIpamIPAddressesPartialUpdateParams()

    params.SetID(ipAddress.ID)
    params.WithData(updates)

    _, err := n.client.Ipam.IpamIPAddressesPartialUpdate(params, nil)
    if err != nil {
        return err
    }
    return nil
}

func IpToWriteable(ip *models.IPAddress) *models.WritableIPAddress {
    status := ip.Status.Value

    tags := []*models.NestedTag{}

    for _, tag := range ip.Tags {
        tags = append(tags, &models.NestedTag{
            Name: tag.Name,
            Slug: tag.Slug,
        })
    }

    writeable := models.WritableIPAddress{
        Address:            ip.Address,
        AssignedObject:     ip.AssignedObject,
        AssignedObjectID:   ip.AssignedObjectID,
        AssignedObjectType: ip.AssignedObjectType,
        Created:            ip.Created,
        CustomFields:       ip.CustomFields,
        Description:        ip.Description,
        Display:            ip.Display,
        DNSName:            ip.DNSName,
        ID:                 ip.ID,
        Tags:               tags,
        Status:             *status,
        URL:                ip.URL,
    }
    return &writeable
}
v0ctor commented 1 year ago

As the library is automatically generated from the Netbox OpenAPI specification, and this issue is specification-related, it should be reported to the Netbox project.

Now, the only field of the WritableIPAddress struct without omitempty is Address. Maybe there's some reason for this, but I don't know.