github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv2/organization.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/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/code.cloudfoundry.org/cli/util/codegen/generate.go Organization codetemplates/delete_async_by_guid.go.template delete_organization.go
    41  //go:generate go run $GOPATH/src/code.cloudfoundry.org/cli/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  	request, err := client.newHTTPRequest(requestOptions{
    66  		RequestName: internal.GetOrganizationsRequest,
    67  		Query:       FormatQueryParameters(queries),
    68  	})
    69  
    70  	if err != nil {
    71  		return nil, nil, err
    72  	}
    73  
    74  	var fullOrgsList []Organization
    75  	warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
    76  		if org, ok := item.(Organization); ok {
    77  			fullOrgsList = append(fullOrgsList, org)
    78  		} else {
    79  			return ccerror.UnknownObjectInListError{
    80  				Expected:   Organization{},
    81  				Unexpected: item,
    82  			}
    83  		}
    84  		return nil
    85  	})
    86  
    87  	return fullOrgsList, warnings, err
    88  }