github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/organization.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"net/url"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
    11  )
    12  
    13  // Organization represents a Cloud Controller Organization.
    14  type Organization struct {
    15  
    16  	// GUID is the unique Organization identifier.
    17  	GUID string
    18  
    19  	// Name is the organization's name.
    20  	Name string
    21  
    22  	// QuotaDefinitionGUID is unique identifier of the quota assigned to this
    23  	// organization.
    24  	QuotaDefinitionGUID string
    25  
    26  	// DefaultIsolationSegmentGUID is the unique identifier of the isolation
    27  	// segment this organization is tagged with.
    28  	DefaultIsolationSegmentGUID string
    29  }
    30  
    31  // UnmarshalJSON helps unmarshal a Cloud Controller Organization response.
    32  func (org *Organization) UnmarshalJSON(data []byte) error {
    33  	var ccOrg struct {
    34  		Metadata internal.Metadata `json:"metadata"`
    35  		Entity   struct {
    36  			Name                        string `json:"name"`
    37  			QuotaDefinitionGUID         string `json:"quota_definition_guid"`
    38  			DefaultIsolationSegmentGUID string `json:"default_isolation_segment_guid"`
    39  		} `json:"entity"`
    40  	}
    41  	err := cloudcontroller.DecodeJSON(data, &ccOrg)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	org.GUID = ccOrg.Metadata.GUID
    47  	org.Name = ccOrg.Entity.Name
    48  	org.QuotaDefinitionGUID = ccOrg.Entity.QuotaDefinitionGUID
    49  	org.DefaultIsolationSegmentGUID = ccOrg.Entity.DefaultIsolationSegmentGUID
    50  	return nil
    51  }
    52  
    53  type createOrganizationRequestBody struct {
    54  	Name                string `json:"name"`
    55  	QuotaDefinitionGUID string `json:"quota_definition_guid,omitempty"`
    56  }
    57  
    58  func (client *Client) CreateOrganization(orgName string, quotaGUID string) (Organization, Warnings, error) {
    59  	requestBody := createOrganizationRequestBody{
    60  		Name:                orgName,
    61  		QuotaDefinitionGUID: quotaGUID,
    62  	}
    63  
    64  	bodyBytes, err := json.Marshal(requestBody)
    65  	if err != nil {
    66  		return Organization{}, nil, err
    67  	}
    68  
    69  	request, err := client.newHTTPRequest(requestOptions{
    70  		RequestName: internal.PostOrganizationRequest,
    71  		Body:        bytes.NewReader(bodyBytes),
    72  	})
    73  
    74  	if err != nil {
    75  		return Organization{}, nil, err
    76  	}
    77  
    78  	var org Organization
    79  	response := cloudcontroller.Response{
    80  		DecodeJSONResponseInto: &org,
    81  	}
    82  
    83  	err = client.connection.Make(request, &response)
    84  	return org, response.Warnings, err
    85  }
    86  
    87  // DeleteOrganization deletes the Organization associated with the provided
    88  // GUID. It will return the Cloud Controller job that is assigned to the
    89  // Organization deletion.
    90  func (client *Client) DeleteOrganization(guid string) (Job, Warnings, error) {
    91  	request, err := client.newHTTPRequest(requestOptions{
    92  		RequestName: internal.DeleteOrganizationRequest,
    93  		URIParams:   Params{"organization_guid": guid},
    94  		Query: url.Values{
    95  			"recursive": {"true"},
    96  			"async":     {"true"},
    97  		},
    98  	})
    99  	if err != nil {
   100  		return Job{}, nil, err
   101  	}
   102  
   103  	var job Job
   104  	response := cloudcontroller.Response{
   105  		DecodeJSONResponseInto: &job,
   106  	}
   107  
   108  	err = client.connection.Make(request, &response)
   109  	return job, response.Warnings, err
   110  }
   111  
   112  // GetOrganization returns an Organization associated with the provided GUID.
   113  func (client *Client) GetOrganization(guid string) (Organization, Warnings, error) {
   114  	request, err := client.newHTTPRequest(requestOptions{
   115  		RequestName: internal.GetOrganizationRequest,
   116  		URIParams:   Params{"organization_guid": guid},
   117  	})
   118  	if err != nil {
   119  		return Organization{}, nil, err
   120  	}
   121  
   122  	var org Organization
   123  	response := cloudcontroller.Response{
   124  		DecodeJSONResponseInto: &org,
   125  	}
   126  
   127  	err = client.connection.Make(request, &response)
   128  	return org, response.Warnings, err
   129  }
   130  
   131  // GetOrganizations returns back a list of Organizations based off of the
   132  // provided filters.
   133  func (client *Client) GetOrganizations(filters ...Filter) ([]Organization, Warnings, error) {
   134  	allQueries := ConvertFilterParameters(filters)
   135  	allQueries.Add("order-by", "name")
   136  	request, err := client.newHTTPRequest(requestOptions{
   137  		RequestName: internal.GetOrganizationsRequest,
   138  		Query:       allQueries,
   139  	})
   140  
   141  	if err != nil {
   142  		return nil, nil, err
   143  	}
   144  
   145  	var fullOrgsList []Organization
   146  	warnings, err := client.paginate(request, Organization{}, func(item interface{}) error {
   147  		if org, ok := item.(Organization); ok {
   148  			fullOrgsList = append(fullOrgsList, org)
   149  		} else {
   150  			return ccerror.UnknownObjectInListError{
   151  				Expected:   Organization{},
   152  				Unexpected: item,
   153  			}
   154  		}
   155  		return nil
   156  	})
   157  
   158  	return fullOrgsList, warnings, err
   159  }
   160  
   161  type updateOrgManagerByUsernameRequestBody struct {
   162  	Username string `json:"username"`
   163  }
   164  
   165  // UpdateOrganizationManager assigns the org manager role to the UAA user or client with the provided ID.
   166  func (client *Client) UpdateOrganizationManager(guid string, uaaID string) (Warnings, error) {
   167  	request, err := client.newHTTPRequest(requestOptions{
   168  		RequestName: internal.PutOrganizationManagerRequest,
   169  		URIParams:   Params{"organization_guid": guid, "manager_guid": uaaID},
   170  	})
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	response := cloudcontroller.Response{}
   176  	err = client.connection.Make(request, &response)
   177  
   178  	return response.Warnings, err
   179  }
   180  
   181  // UpdateOrganizationManagerByUsername assigns the org manager role to the user with the provided name.
   182  func (client *Client) UpdateOrganizationManagerByUsername(guid string, username string) (Warnings, error) {
   183  	requestBody := updateOrgManagerByUsernameRequestBody{
   184  		Username: username,
   185  	}
   186  
   187  	body, err := json.Marshal(requestBody)
   188  	if err != nil {
   189  		return nil, err
   190  	}
   191  
   192  	request, err := client.newHTTPRequest(requestOptions{
   193  		RequestName: internal.PutOrganizationManagerByUsernameRequest,
   194  		Body:        bytes.NewReader(body),
   195  		URIParams:   Params{"organization_guid": guid},
   196  	})
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	response := cloudcontroller.Response{}
   202  	err = client.connection.Make(request, &response)
   203  
   204  	return response.Warnings, err
   205  }
   206  
   207  // UpdateOrganizationUser makes the user or client with the given UAA ID a
   208  // member of the org.
   209  func (client *Client) UpdateOrganizationUser(guid string, uaaID string) (Warnings, error) {
   210  	request, err := client.newHTTPRequest(requestOptions{
   211  		RequestName: internal.PutOrganizationUserRequest,
   212  		URIParams:   Params{"organization_guid": guid, "user_guid": uaaID},
   213  	})
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	response := cloudcontroller.Response{}
   219  	err = client.connection.Make(request, &response)
   220  
   221  	return response.Warnings, err
   222  }
   223  
   224  type updateOrgUserByUsernameRequestBody struct {
   225  	Username string `json:"username"`
   226  }
   227  
   228  // UpdateOrganizationUserByUsername makes the user with the given username a member of
   229  // the org.
   230  func (client Client) UpdateOrganizationUserByUsername(orgGUID string, username string) (Warnings, error) {
   231  	requestBody := updateOrgUserByUsernameRequestBody{
   232  		Username: username,
   233  	}
   234  
   235  	body, err := json.Marshal(requestBody)
   236  	if err != nil {
   237  		return Warnings{}, err
   238  	}
   239  
   240  	request, err := client.newHTTPRequest(requestOptions{
   241  		RequestName: internal.PutOrganizationUserByUsernameRequest,
   242  		Body:        bytes.NewReader(body),
   243  		URIParams:   Params{"organization_guid": orgGUID},
   244  	})
   245  	if err != nil {
   246  		return Warnings{}, err
   247  	}
   248  
   249  	response := cloudcontroller.Response{}
   250  	err = client.connection.Make(request, &response)
   251  
   252  	return response.Warnings, err
   253  }