github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv2/user.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     9  )
    10  
    11  // User represents a Cloud Controller User.
    12  type User struct {
    13  	GUID string
    14  }
    15  
    16  // userRequestBody represents the body of the request.
    17  type userRequestBody struct {
    18  	GUID string `json:"guid"`
    19  }
    20  
    21  // UnmarshalJSON helps unmarshal a Cloud Controller User response.
    22  func (user *User) UnmarshalJSON(data []byte) error {
    23  	var ccUser struct {
    24  		Metadata internal.Metadata `json:"metadata"`
    25  	}
    26  	if err := json.Unmarshal(data, &ccUser); err != nil {
    27  		return err
    28  	}
    29  
    30  	user.GUID = ccUser.Metadata.GUID
    31  	return nil
    32  }
    33  
    34  // CreateUser creates a new Cloud Controller User from the provided UAA user
    35  // ID.
    36  func (client *Client) CreateUser(uaaUserID string) (User, Warnings, error) {
    37  	bodyBytes, err := json.Marshal(userRequestBody{
    38  		GUID: uaaUserID,
    39  	})
    40  	if err != nil {
    41  		return User{}, nil, err
    42  	}
    43  
    44  	request, err := client.newHTTPRequest(requestOptions{
    45  		RequestName: internal.PostUserRequest,
    46  		Body:        bytes.NewReader(bodyBytes),
    47  	})
    48  	if err != nil {
    49  		return User{}, nil, err
    50  	}
    51  
    52  	var user User
    53  	response := cloudcontroller.Response{
    54  		Result: &user,
    55  	}
    56  
    57  	err = client.connection.Make(request, &response)
    58  	if err != nil {
    59  		return User{}, response.Warnings, err
    60  	}
    61  
    62  	return user, response.Warnings, nil
    63  }