github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/api/cloudcontroller/ccv2/organization.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"net/url"
     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  	err := cloudcontroller.DecodeJSON(data, &ccOrg)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	org.GUID = ccOrg.Metadata.GUID
    45  	org.Name = ccOrg.Entity.Name
    46  	org.QuotaDefinitionGUID = ccOrg.Entity.QuotaDefinitionGUID
    47  	org.DefaultIsolationSegmentGUID = ccOrg.Entity.DefaultIsolationSegmentGUID
    48  	return nil
    49  }
    50  
    51  // DeleteOrganization deletes the Organization associated with the provided
    52  // GUID. It will return the Cloud Controller job that is assigned to the
    53  // Organization deletion.
    54  func (client *Client) DeleteOrganization(guid string) (Job, Warnings, error) {
    55  	request, err := client.newHTTPRequest(requestOptions{
    56  		RequestName: internal.DeleteOrganizationRequest,
    57  		URIParams:   Params{"organization_guid": guid},
    58  		Query: url.Values{
    59  			"recursive": {"true"},
    60  			"async":     {"true"},
    61  		},
    62  	})
    63  	if err != nil {
    64  		return Job{}, nil, err
    65  	}
    66  
    67  	var job Job
    68  	response := cloudcontroller.Response{
    69  		Result: &job,
    70  	}
    71  
    72  	err = client.connection.Make(request, &response)
    73  	return job, response.Warnings, err
    74  }
    75  
    76  // GetOrganization returns an Organization associated with the provided GUID.
    77  func (client *Client) GetOrganization(guid string) (Organization, Warnings, error) {
    78  	request, err := client.newHTTPRequest(requestOptions{
    79  		RequestName: internal.GetOrganizationRequest,
    80  		URIParams:   Params{"organization_guid": guid},
    81  	})
    82  	if err != nil {
    83  		return Organization{}, nil, err
    84  	}
    85  
    86  	var org Organization
    87  	response := cloudcontroller.Response{
    88  		Result: &org,
    89  	}
    90  
    91  	err = client.connection.Make(request, &response)
    92  	return org, response.Warnings, err
    93  }
    94  
    95  // GetOrganizations returns back a list of Organizations based off of the
    96  // provided filters.
    97  func (client *Client) GetOrganizations(filters ...Filter) ([]Organization, Warnings, error) {
    98  	allQueries := ConvertFilterParameters(filters)
    99  	allQueries.Add("order-by", "name")
   100  	request, err := client.newHTTPRequest(requestOptions{
   101  		RequestName: internal.GetOrganizationsRequest,
   102  		Query:       allQueries,
   103  	})
   104  
   105  	if err != nil {
   106  		return nil, nil, err
   107  	}
   108  
   109  	var fullOrgsList []Organization
   110  	warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
   111  		if org, ok := item.(Organization); ok {
   112  			fullOrgsList = append(fullOrgsList, org)
   113  		} else {
   114  			return ccerror.UnknownObjectInListError{
   115  				Expected:   Organization{},
   116  				Unexpected: item,
   117  			}
   118  		}
   119  		return nil
   120  	})
   121  
   122  	return fullOrgsList, warnings, err
   123  }