2018-06-29 22:53:14 +00:00
|
|
|
package itglue
|
|
|
|
|
2018-06-30 04:19:49 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-06-30 05:54:16 +00:00
|
|
|
"net/url"
|
2018-06-30 04:19:49 +00:00
|
|
|
)
|
2018-06-29 22:53:14 +00:00
|
|
|
|
2018-07-22 07:45:09 +00:00
|
|
|
//OrganizationData contains the schema of an Organization in IT Glue without the "data" wrapper.
|
2018-06-30 05:37:34 +00:00
|
|
|
//This allows us to reuse the schema when data is either a JSON object or an array, depending on what results are returned
|
2018-07-22 07:45:09 +00:00
|
|
|
type OrganizationData struct {
|
2018-06-30 05:37:34 +00:00
|
|
|
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
|
2018-06-29 22:53:14 +00:00
|
|
|
type Organization struct {
|
2018-07-22 07:45:09 +00:00
|
|
|
Data struct{ OrganizationData } `json:"data"`
|
|
|
|
Meta struct{ Metadata } `json:"metadata"`
|
2018-06-30 05:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//OrganizationList contains a slice of Organizations
|
|
|
|
type OrganizationList struct {
|
2018-07-22 07:45:09 +00:00
|
|
|
Data []struct{ OrganizationData } `json:"data"`
|
|
|
|
Meta struct{ Metadata } `json:"metadata"`
|
|
|
|
}
|
|
|
|
|
2018-06-29 22:53:14 +00:00
|
|
|
//GetOrganizationByID expects an ITG organization ID
|
|
|
|
//Returns a pointer to an Organization struct
|
2018-07-22 07:22:22 +00:00
|
|
|
func (itg *ITGAPI) GetOrganizationByID(organizationID int) (*Organization, error) {
|
2018-07-22 07:41:33 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-06-30 04:19:49 +00:00
|
|
|
organization := &Organization{}
|
2018-07-22 07:41:33 +00:00
|
|
|
err = json.Unmarshal(body, organization)
|
2018-07-22 07:22:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get organization: %s", err)
|
|
|
|
}
|
|
|
|
return organization, nil
|
2018-06-30 05:54:16 +00:00
|
|
|
}
|
2018-06-29 22:53:14 +00:00
|
|
|
|
2018-06-30 05:54:16 +00:00
|
|
|
//GetOrganizationByName expects an exact matching organization name and returns an OrganizationList
|
2018-07-22 07:22:22 +00:00
|
|
|
func (itg *ITGAPI) GetOrganizationByName(organizationName string) (*OrganizationList, error) {
|
2018-07-22 07:41:33 +00:00
|
|
|
itgurl, err := itg.BuildURL("/organizations")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not build URL: %s", err)
|
|
|
|
}
|
2018-06-30 05:54:16 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("filter[name]", organizationName)
|
|
|
|
itgurl.RawQuery = params.Encode()
|
2018-07-22 07:41:33 +00:00
|
|
|
body, err := itg.GetRequest(itgurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("request failed: %s", err)
|
|
|
|
}
|
2018-06-30 05:54:16 +00:00
|
|
|
organization := &OrganizationList{}
|
2018-07-22 07:41:33 +00:00
|
|
|
err = json.Unmarshal(body, organization)
|
2018-07-22 07:22:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get organization: %s", err)
|
|
|
|
}
|
|
|
|
return organization, nil
|
2018-06-29 22:53:14 +00:00
|
|
|
}
|