1

Apologies for the title, but this is a weird one and out of my ability to understand.

I'm working with a go library that's sort of finished, but sort of not:

https://github.com/yfronto/go-statuspage-api

The statuspage.io API supports the following params when posting an incident:

incident[components][component_id] - Map of status changes to apply to affected components.

An example would be:

"incident[components][ftgks51sfs2d]=degraded_performance"

Unfortunately, the struct defined in the library doesn't support that particular field:

type NewIncidentUpdate struct {
    Name               string
    Status             string
    Message            string
    WantsTwitterUpdate bool
    ImpactOverride     string
    ComponentIDs       []string
}

func (i *NewIncidentUpdate) String() string {
    return encodeParams(map[string]interface{}{
        "incident[name]":                 i.Name,
        "incident[status]":               i.Status,
        "incident[message]":              i.Message,
        "incident[wants_twitter_update]": i.WantsTwitterUpdate,
        "incident[impact_override]":      i.ImpactOverride,
        "incident[component_ids]":        i.ComponentIDs,
    })
}

How can I update this struct (and the associated encodeParams function) to support passing an arbitrary map of components and associated statuses?

1 Answer 1

1

You could embed a NewIncidentUpdate inside your own struct that defines the component status changes.

type MyIncidentUpdate struct {
    NewIncidentUpdate
    ComponentStatusChanges map[string]string
}

then define String the same way, while accomodating for your ComponentStatusChanges map.

func (i *MyIncidentUpdate) String() string {
    params := map[string]interface{}{
        "incident[name]":                 i.Name,
        "incident[status]":               i.Status,
        "incident[message]":              i.Message,
        "incident[wants_twitter_update]": i.WantsTwitterUpdate,
        "incident[impact_override]":      i.ImpactOverride,
        "incident[component_ids]":        i.ComponentIDs,
    }
    for k, val := range i.ComponentStatusChanges {
        key := fmt.Sprintf("incident[components][%s]", k)
        params[key] = val
    }

    return encodeParams(params)
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is assuming I didn't want to modify the existing library, but I'm quite happy to fork the repo and update the repo in place, is there a way to do that?
@jaxxstorm if they accept your PR, then sure. Simply change their NewIncidentUpdate to include the status changes map and update the String method (and any other method that might be affected -- I haven't scoured the repo) accordingly. (and good on you for contributing to existing projects!)
Thanks for the fantastic answer, really appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.