80 lines
2.7 KiB
Go
80 lines
2.7 KiB
Go
package itglue
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
//OrganizationData contains the schema of an Organization in IT Glue without the "data" wrapper.
|
|
//This allows us to reuse the schema when data is either a JSON object or an array, depending on what results are returned
|
|
type OrganizationData struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Attributes struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
OrganizationTypeID int `json:"organization-type-id"`
|
|
OrganizationTypeName string `json:"organization-type-name"`
|
|
OrganizationStatusID int `json:"organization-status-id"`
|
|
OrganizationStatusName string `json:"organization-status-name"`
|
|
Logo string `json:"logo"`
|
|
QuickNotes string `json:"quick-notes"`
|
|
ShortName string `json:"short-name"`
|
|
CreatedAt string `json:"created-at"`
|
|
UpdatedAt string `json:"updated-at"`
|
|
} `json:"attributes"`
|
|
}
|
|
|
|
//Organization contains a single Organization
|
|
type Organization struct {
|
|
Data struct{ OrganizationData } `json:"data"`
|
|
Meta struct{ Metadata } `json:"metadata"`
|
|
}
|
|
|
|
//OrganizationList contains a slice of Organizations
|
|
type OrganizationList struct {
|
|
Data []struct{ OrganizationData } `json:"data"`
|
|
Meta struct{ Metadata } `json:"metadata"`
|
|
}
|
|
|
|
//GetOrganizationByID expects an ITG organization ID
|
|
//Returns a pointer to an Organization struct
|
|
func (itg *ITGAPI) GetOrganizationByID(organizationID int) (*Organization, error) {
|
|
itgurl, err := itg.BuildURL(fmt.Sprintf("/organizations/%d", organizationID))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not build URL: %s", err)
|
|
}
|
|
body, err := itg.GetRequest(itgurl)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("request failed: %s", err)
|
|
}
|
|
organization := &Organization{}
|
|
err = json.Unmarshal(body, organization)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not get organization: %s", err)
|
|
}
|
|
return organization, nil
|
|
}
|
|
|
|
//GetOrganizationByName expects an exact matching organization name and returns an OrganizationList
|
|
func (itg *ITGAPI) GetOrganizationByName(organizationName string) (*OrganizationList, error) {
|
|
itgurl, err := itg.BuildURL("/organizations")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not build URL: %s", err)
|
|
}
|
|
params := url.Values{}
|
|
params.Add("filter[name]", organizationName)
|
|
itgurl.RawQuery = params.Encode()
|
|
body, err := itg.GetRequest(itgurl)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("request failed: %s", err)
|
|
}
|
|
organization := &OrganizationList{}
|
|
err = json.Unmarshal(body, organization)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not get organization: %s", err)
|
|
}
|
|
return organization, nil
|
|
}
|