github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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  // NewUser creates a new Cloud Controller User from the provided UAA user ID.
    35  func (client *Client) NewUser(uaaUserID string) (User, Warnings, error) {
    36  	bodyBytes, err := json.Marshal(userRequestBody{
    37  		GUID: uaaUserID,
    38  	})
    39  	if err != nil {
    40  		return User{}, nil, err
    41  	}
    42  
    43  	request, err := client.newHTTPRequest(requestOptions{
    44  		RequestName: internal.UsersRequest,
    45  		Body:        bytes.NewBuffer(bodyBytes),
    46  	})
    47  	if err != nil {
    48  		return User{}, nil, err
    49  	}
    50  
    51  	var user User
    52  	response := cloudcontroller.Response{
    53  		Result: &user,
    54  	}
    55  
    56  	err = client.connection.Make(request, &response)
    57  	if err != nil {
    58  		return User{}, response.Warnings, err
    59  	}
    60  
    61  	return user, response.Warnings, nil
    62  }