github.com/orange-cloudfoundry/cli@v7.1.0+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 is the unique user identifier.
    14  	GUID string
    15  }
    16  
    17  // UnmarshalJSON helps unmarshal a Cloud Controller User response.
    18  func (user *User) UnmarshalJSON(data []byte) error {
    19  	var ccUser struct {
    20  		Metadata internal.Metadata `json:"metadata"`
    21  	}
    22  	err := cloudcontroller.DecodeJSON(data, &ccUser)
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	user.GUID = ccUser.Metadata.GUID
    28  	return nil
    29  }
    30  
    31  // CreateUser creates a new Cloud Controller User from the provided UAA user
    32  // ID.
    33  func (client *Client) CreateUser(uaaUserID string) (User, Warnings, error) {
    34  	type userRequestBody struct {
    35  		GUID string `json:"guid"`
    36  	}
    37  
    38  	bodyBytes, err := json.Marshal(userRequestBody{
    39  		GUID: uaaUserID,
    40  	})
    41  	if err != nil {
    42  		return User{}, nil, err
    43  	}
    44  
    45  	request, err := client.newHTTPRequest(requestOptions{
    46  		RequestName: internal.PostUserRequest,
    47  		Body:        bytes.NewReader(bodyBytes),
    48  	})
    49  	if err != nil {
    50  		return User{}, nil, err
    51  	}
    52  
    53  	var user User
    54  	response := cloudcontroller.Response{
    55  		DecodeJSONResponseInto: &user,
    56  	}
    57  
    58  	err = client.connection.Make(request, &response)
    59  	if err != nil {
    60  		return User{}, response.Warnings, err
    61  	}
    62  
    63  	return user, response.Warnings, nil
    64  }