github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/api/cloudcontroller/ccv2/organization.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller"
     7  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror"
     8  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2/internal"
     9  )
    10  
    11  // Organization represents a Cloud Controller Organization.
    12  type Organization struct {
    13  	GUID                        string
    14  	Name                        string
    15  	QuotaDefinitionGUID         string
    16  	DefaultIsolationSegmentGUID string
    17  }
    18  
    19  // UnmarshalJSON helps unmarshal a Cloud Controller Organization response.
    20  func (org *Organization) UnmarshalJSON(data []byte) error {
    21  	var ccOrg struct {
    22  		Metadata internal.Metadata `json:"metadata"`
    23  		Entity   struct {
    24  			Name                        string `json:"name"`
    25  			QuotaDefinitionGUID         string `json:"quota_definition_guid"`
    26  			DefaultIsolationSegmentGUID string `json:"default_isolation_segment_guid"`
    27  		} `json:"entity"`
    28  	}
    29  	if err := json.Unmarshal(data, &ccOrg); err != nil {
    30  		return err
    31  	}
    32  
    33  	org.GUID = ccOrg.Metadata.GUID
    34  	org.Name = ccOrg.Entity.Name
    35  	org.QuotaDefinitionGUID = ccOrg.Entity.QuotaDefinitionGUID
    36  	org.DefaultIsolationSegmentGUID = ccOrg.Entity.DefaultIsolationSegmentGUID
    37  	return nil
    38  }
    39  
    40  //go:generate go run $GOPATH/src/github.com/liamawhite/cli-with-i18n/util/codegen/generate.go Organization codetemplates/delete_async_by_guid.go.template delete_organization.go
    41  //go:generate go run $GOPATH/src/github.com/liamawhite/cli-with-i18n/util/codegen/generate.go Organization codetemplates/delete_async_by_guid_test.go.template delete_organization_test.go
    42  
    43  // GetOrganization returns an Organization associated with the provided guid.
    44  func (client *Client) GetOrganization(guid string) (Organization, Warnings, error) {
    45  	request, err := client.newHTTPRequest(requestOptions{
    46  		RequestName: internal.GetOrganizationRequest,
    47  		URIParams:   Params{"organization_guid": guid},
    48  	})
    49  	if err != nil {
    50  		return Organization{}, nil, err
    51  	}
    52  
    53  	var org Organization
    54  	response := cloudcontroller.Response{
    55  		Result: &org,
    56  	}
    57  
    58  	err = client.connection.Make(request, &response)
    59  	return org, response.Warnings, err
    60  }
    61  
    62  // GetOrganizations returns back a list of Organizations based off of the
    63  // provided queries.
    64  func (client *Client) GetOrganizations(queries ...Query) ([]Organization, Warnings, error) {
    65  	allQueries := FormatQueryParameters(queries)
    66  	allQueries.Add("order-by", "name")
    67  	request, err := client.newHTTPRequest(requestOptions{
    68  		RequestName: internal.GetOrganizationsRequest,
    69  		Query:       allQueries,
    70  	})
    71  
    72  	if err != nil {
    73  		return nil, nil, err
    74  	}
    75  
    76  	var fullOrgsList []Organization
    77  	warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
    78  		if org, ok := item.(Organization); ok {
    79  			fullOrgsList = append(fullOrgsList, org)
    80  		} else {
    81  			return ccerror.UnknownObjectInListError{
    82  				Expected:   Organization{},
    83  				Unexpected: item,
    84  			}
    85  		}
    86  		return nil
    87  	})
    88  
    89  	return fullOrgsList, warnings, err
    90  }