github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  
    14  	// GUID is the unique Organization identifier.
    15  	GUID string
    16  
    17  	// Name is the organization's name.
    18  	Name string
    19  
    20  	// QuotaDefinitionGUID is unique identifier of the quota assigned to this
    21  	// organization.
    22  	QuotaDefinitionGUID string
    23  
    24  	// DefaultIsolationSegmentGUID is the unique identifier of the isolation
    25  	// segment this organization is tagged with.
    26  	DefaultIsolationSegmentGUID string
    27  }
    28  
    29  // UnmarshalJSON helps unmarshal a Cloud Controller Organization response.
    30  func (org *Organization) UnmarshalJSON(data []byte) error {
    31  	var ccOrg struct {
    32  		Metadata internal.Metadata `json:"metadata"`
    33  		Entity   struct {
    34  			Name                        string `json:"name"`
    35  			QuotaDefinitionGUID         string `json:"quota_definition_guid"`
    36  			DefaultIsolationSegmentGUID string `json:"default_isolation_segment_guid"`
    37  		} `json:"entity"`
    38  	}
    39  	if err := json.Unmarshal(data, &ccOrg); err != nil {
    40  		return err
    41  	}
    42  
    43  	org.GUID = ccOrg.Metadata.GUID
    44  	org.Name = ccOrg.Entity.Name
    45  	org.QuotaDefinitionGUID = ccOrg.Entity.QuotaDefinitionGUID
    46  	org.DefaultIsolationSegmentGUID = ccOrg.Entity.DefaultIsolationSegmentGUID
    47  	return nil
    48  }
    49  
    50  //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
    51  //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
    52  
    53  // GetOrganization returns an Organization associated with the provided GUID.
    54  func (client *Client) GetOrganization(guid string) (Organization, Warnings, error) {
    55  	request, err := client.newHTTPRequest(requestOptions{
    56  		RequestName: internal.GetOrganizationRequest,
    57  		URIParams:   Params{"organization_guid": guid},
    58  	})
    59  	if err != nil {
    60  		return Organization{}, nil, err
    61  	}
    62  
    63  	var org Organization
    64  	response := cloudcontroller.Response{
    65  		Result: &org,
    66  	}
    67  
    68  	err = client.connection.Make(request, &response)
    69  	return org, response.Warnings, err
    70  }
    71  
    72  // GetOrganizations returns back a list of Organizations based off of the
    73  // provided filters.
    74  func (client *Client) GetOrganizations(filters ...Filter) ([]Organization, Warnings, error) {
    75  	allQueries := ConvertFilterParameters(filters)
    76  	allQueries.Add("order-by", "name")
    77  	request, err := client.newHTTPRequest(requestOptions{
    78  		RequestName: internal.GetOrganizationsRequest,
    79  		Query:       allQueries,
    80  	})
    81  
    82  	if err != nil {
    83  		return nil, nil, err
    84  	}
    85  
    86  	var fullOrgsList []Organization
    87  	warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
    88  		if org, ok := item.(Organization); ok {
    89  			fullOrgsList = append(fullOrgsList, org)
    90  		} else {
    91  			return ccerror.UnknownObjectInListError{
    92  				Expected:   Organization{},
    93  				Unexpected: item,
    94  			}
    95  		}
    96  		return nil
    97  	})
    98  
    99  	return fullOrgsList, warnings, err
   100  }