github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/organization.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    10  )
    11  
    12  // Organization represents a Cloud Controller V3 Organization.
    13  type Organization struct {
    14  	// GUID is the unique organization identifier.
    15  	GUID string `json:"guid,omitempty"`
    16  	// Name is the name of the organization.
    17  	Name string `json:"name"`
    18  
    19  	// Metadata is used for custom tagging of API resources
    20  	Metadata *Metadata `json:"metadata,omitempty"`
    21  }
    22  
    23  // CreateOrganization creates an organization with the given name.
    24  func (client *Client) CreateOrganization(orgName string) (Organization, Warnings, error) {
    25  	org := Organization{Name: orgName}
    26  	orgBytes, err := json.Marshal(org)
    27  	if err != nil {
    28  		return Organization{}, nil, err
    29  	}
    30  
    31  	request, err := client.newHTTPRequest(requestOptions{
    32  		RequestName: internal.PostOrganizationRequest,
    33  		Body:        bytes.NewReader(orgBytes),
    34  	})
    35  
    36  	if err != nil {
    37  		return Organization{}, nil, err
    38  	}
    39  
    40  	var responseOrg Organization
    41  	response := cloudcontroller.Response{
    42  		DecodeJSONResponseInto: &responseOrg,
    43  	}
    44  	err = client.connection.Make(request, &response)
    45  
    46  	if err != nil {
    47  		return Organization{}, response.Warnings, err
    48  	}
    49  
    50  	return responseOrg, response.Warnings, err
    51  }
    52  
    53  // DeleteOrganization deletes the organization with the given GUID.
    54  func (client *Client) DeleteOrganization(orgGUID string) (JobURL, Warnings, error) {
    55  	request, err := client.newHTTPRequest(requestOptions{
    56  		RequestName: internal.DeleteOrganizationRequest,
    57  		URIParams:   map[string]string{"organization_guid": orgGUID},
    58  	})
    59  
    60  	if err != nil {
    61  		return "", nil, err
    62  	}
    63  
    64  	response := cloudcontroller.Response{}
    65  	err = client.connection.Make(request, &response)
    66  	return JobURL(response.ResourceLocationURL), response.Warnings, err
    67  }
    68  
    69  // GetDefaultDomain gets the default domain for the organization with the given GUID.
    70  func (client *Client) GetDefaultDomain(orgGUID string) (Domain, Warnings, error) {
    71  	request, err := client.newHTTPRequest(requestOptions{
    72  		RequestName: internal.GetDefaultDomainRequest,
    73  		URIParams:   map[string]string{"organization_guid": orgGUID},
    74  	})
    75  	if err != nil {
    76  		return Domain{}, nil, err
    77  	}
    78  
    79  	var defaultDomain Domain
    80  
    81  	response := cloudcontroller.Response{
    82  		DecodeJSONResponseInto: &defaultDomain,
    83  	}
    84  
    85  	err = client.connection.Make(request, &response)
    86  
    87  	return defaultDomain, response.Warnings, err
    88  }
    89  
    90  // GetIsolationSegmentOrganizations lists organizations
    91  // entitled to an isolation segment.
    92  func (client *Client) GetIsolationSegmentOrganizations(isolationSegmentGUID string) ([]Organization, Warnings, error) {
    93  	request, err := client.newHTTPRequest(requestOptions{
    94  		RequestName: internal.GetIsolationSegmentOrganizationsRequest,
    95  		URIParams:   map[string]string{"isolation_segment_guid": isolationSegmentGUID},
    96  	})
    97  	if err != nil {
    98  		return nil, nil, err
    99  	}
   100  
   101  	var fullOrgsList []Organization
   102  	warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
   103  		if app, ok := item.(Organization); ok {
   104  			fullOrgsList = append(fullOrgsList, app)
   105  		} else {
   106  			return ccerror.UnknownObjectInListError{
   107  				Expected:   Organization{},
   108  				Unexpected: item,
   109  			}
   110  		}
   111  		return nil
   112  	})
   113  
   114  	return fullOrgsList, warnings, err
   115  }
   116  
   117  // GetOrganization gets an organization by the given guid.
   118  func (client *Client) GetOrganization(orgGUID string) (Organization, Warnings, error) {
   119  	request, err := client.newHTTPRequest(requestOptions{
   120  		RequestName: internal.GetOrganizationRequest,
   121  		URIParams:   map[string]string{"organization_guid": orgGUID},
   122  	})
   123  
   124  	if err != nil {
   125  		return Organization{}, nil, err
   126  	}
   127  
   128  	var responseOrg Organization
   129  	response := cloudcontroller.Response{
   130  		DecodeJSONResponseInto: &responseOrg,
   131  	}
   132  	err = client.connection.Make(request, &response)
   133  	if err != nil {
   134  		return Organization{}, response.Warnings, err
   135  	}
   136  
   137  	return responseOrg, response.Warnings, nil
   138  }
   139  
   140  // GetOrganizations lists organizations with optional filters.
   141  func (client *Client) GetOrganizations(query ...Query) ([]Organization, Warnings, error) {
   142  	request, err := client.newHTTPRequest(requestOptions{
   143  		RequestName: internal.GetOrganizationsRequest,
   144  		Query:       query,
   145  	})
   146  	if err != nil {
   147  		return nil, nil, err
   148  	}
   149  
   150  	var fullOrgsList []Organization
   151  	warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
   152  		if app, ok := item.(Organization); ok {
   153  			fullOrgsList = append(fullOrgsList, app)
   154  		} else {
   155  			return ccerror.UnknownObjectInListError{
   156  				Expected:   Organization{},
   157  				Unexpected: item,
   158  			}
   159  		}
   160  		return nil
   161  	})
   162  
   163  	return fullOrgsList, warnings, err
   164  }
   165  
   166  // UpdateOrganization updates an organization with the given properties.
   167  func (client *Client) UpdateOrganization(org Organization) (Organization, Warnings, error) {
   168  	orgGUID := org.GUID
   169  	org.GUID = ""
   170  	orgBytes, err := json.Marshal(org)
   171  	if err != nil {
   172  		return Organization{}, nil, err
   173  	}
   174  	request, err := client.newHTTPRequest(requestOptions{
   175  		RequestName: internal.PatchOrganizationRequest,
   176  		Body:        bytes.NewReader(orgBytes),
   177  		URIParams:   map[string]string{"organization_guid": orgGUID},
   178  	})
   179  
   180  	if err != nil {
   181  		return Organization{}, nil, err
   182  	}
   183  
   184  	var responseOrg Organization
   185  	response := cloudcontroller.Response{
   186  		DecodeJSONResponseInto: &responseOrg,
   187  	}
   188  	err = client.connection.Make(request, &response)
   189  
   190  	if err != nil {
   191  		return Organization{}, nil, err
   192  	}
   193  	return responseOrg, nil, err
   194  }