| 1 | package opsgenie |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Entity represents an OpsGenie entity (e.g., user, team) with type and identifier. |
| 11 | // Example JSON: { "username":"[email protected]", "type":"user" }. |
| 12 | type Entity struct { |
| 13 | Type string `json:"type"` |
| 14 | ID string `json:"id,omitempty"` |
| 15 | Name string `json:"name,omitempty"` |
| 16 | Username string `json:"username,omitempty"` |
| 17 | } |
| 18 | |
| 19 | // EntityPartsCount is the expected number of parts in an entity string (type:identifier). |
| 20 | const ( |
| 21 | EntityPartsCount = 2 // Expected number of parts in an entity string (type:identifier) |
| 22 | ) |
| 23 | |
| 24 | // ErrInvalidEntityFormat indicates that the entity string does not have two elements separated by a colon. |
| 25 | var ( |
| 26 | ErrInvalidEntityFormat = errors.New( |
| 27 | "invalid entity, should have two elements separated by colon", |
| 28 | ) |
| 29 | ErrInvalidEntityIDName = errors.New("invalid entity, cannot parse id/name") |
| 30 | ErrUnexpectedEntityType = errors.New("invalid entity, unexpected entity type") |
| 31 | ErrMissingEntityIdentity = errors.New("invalid entity, should have either ID, name or username") |
| 32 | ) |
| 33 | |
| 34 | // GetPropValue serializes an entity back into a string in the format "type:identifier". |
| 35 | func (e *Entity) GetPropValue() (string, error) { |
| 36 | var identifier string |
| 37 | |
| 38 | switch { |
| 39 | case e.ID != "": |
| 40 | identifier = e.ID |
| 41 | case e.Name != "": |
| 42 | identifier = e.Name |
| 43 | case e.Username != "": |
| 44 | identifier = e.Username |
| 45 | default: |
| 46 | return "", ErrMissingEntityIdentity |
| 47 | } |
| 48 | |
| 49 | return fmt.Sprintf("%s:%s", e.Type, identifier), nil |
| 50 | } |
| 51 | |
| 52 | // SetFromProp deserializes an entity from a string in the format "type:identifier". |
| 53 | func (e *Entity) SetFromProp(propValue string) error { |
| 54 | elements := strings.Split(propValue, ":") |
| 55 | |
| 56 | if len(elements) != EntityPartsCount { |
| 57 | return fmt.Errorf("%w: %q", ErrInvalidEntityFormat, propValue) |
| 58 | } |
| 59 | |
| 60 | e.Type = elements[0] |
| 61 | identifier := elements[1] |
| 62 | |
| 63 | isID, err := isOpsGenieID(identifier) |
| 64 | if err != nil { |
| 65 | return fmt.Errorf("%w: %q", ErrInvalidEntityIDName, identifier) |
| 66 | } |
| 67 | |
| 68 | switch { |
| 69 | case isID: |
| 70 | e.ID = identifier |
| 71 | case e.Type == "team": |
| 72 | e.Name = identifier |
| 73 | case e.Type == "user": |
| 74 | e.Username = identifier |
| 75 | default: |
| 76 | return fmt.Errorf("%w: %q", ErrUnexpectedEntityType, e.Type) |
| 77 | } |
| 78 | |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | // isOpsGenieID checks if a string matches the OpsGenie ID format (e.g., 4513b7ea-3b91-438f-b7e4-e3e54af9147c). |
| 83 | func isOpsGenieID(str string) (bool, error) { |
| 84 | matched, err := regexp.MatchString( |
| 85 | `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, |
| 86 | str, |
| 87 | ) |
| 88 | if err != nil { |
| 89 | return false, fmt.Errorf("matching OpsGenie ID format for %q: %w", str, err) |
| 90 | } |
| 91 | |
| 92 | return matched, nil |
| 93 | } |