github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/api/cloudcontroller/ccv3/isolation_segment.go (about)

     1  package ccv3
     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/ccv3/internal"
    11  )
    12  
    13  // IsolationSegment represents a Cloud Controller Isolation Segment.
    14  type IsolationSegment struct {
    15  	Name string `json:"name"`
    16  	GUID string `json:"guid,omitempty"`
    17  }
    18  
    19  // CreateIsolationSegment will create an Isolation Segment on the Cloud
    20  // Controller. Note: This will not validate that the placement tag exists in
    21  // the diego cluster.
    22  func (client *Client) CreateIsolationSegment(isolationSegment IsolationSegment) (IsolationSegment, Warnings, error) {
    23  	body, err := json.Marshal(isolationSegment)
    24  	if err != nil {
    25  		return IsolationSegment{}, nil, err
    26  	}
    27  
    28  	request, err := client.newHTTPRequest(requestOptions{
    29  		RequestName: internal.PostIsolationSegmentsRequest,
    30  		Body:        bytes.NewReader(body),
    31  	})
    32  	if err != nil {
    33  		return IsolationSegment{}, nil, err
    34  	}
    35  
    36  	var responseIsolationSegment IsolationSegment
    37  	response := cloudcontroller.Response{
    38  		Result: &responseIsolationSegment,
    39  	}
    40  
    41  	err = client.connection.Make(request, &response)
    42  	return responseIsolationSegment, response.Warnings, err
    43  }
    44  
    45  // GetIsolationSegments lists isolation segments with optional filters.
    46  func (client *Client) GetIsolationSegments(query url.Values) ([]IsolationSegment, Warnings, error) {
    47  	request, err := client.newHTTPRequest(requestOptions{
    48  		RequestName: internal.GetIsolationSegmentsRequest,
    49  		Query:       query,
    50  	})
    51  	if err != nil {
    52  		return nil, nil, err
    53  	}
    54  
    55  	var fullIsolationSegmentsList []IsolationSegment
    56  	warnings, err := client.paginate(request, IsolationSegment{}, func(item interface{}) error {
    57  		if isolationSegment, ok := item.(IsolationSegment); ok {
    58  			fullIsolationSegmentsList = append(fullIsolationSegmentsList, isolationSegment)
    59  		} else {
    60  			return ccerror.UnknownObjectInListError{
    61  				Expected:   IsolationSegment{},
    62  				Unexpected: item,
    63  			}
    64  		}
    65  		return nil
    66  	})
    67  
    68  	return fullIsolationSegmentsList, warnings, err
    69  }
    70  
    71  // GetIsolationSegment returns back the requested isolation segment that
    72  // matches the GUID.
    73  func (client *Client) GetIsolationSegment(guid string) (IsolationSegment, Warnings, error) {
    74  	request, err := client.newHTTPRequest(requestOptions{
    75  		RequestName: internal.GetIsolationSegmentRequest,
    76  		URIParams:   map[string]string{"isolation_segment_guid": guid},
    77  	})
    78  	if err != nil {
    79  		return IsolationSegment{}, nil, err
    80  	}
    81  	var isolationSegment IsolationSegment
    82  	response := cloudcontroller.Response{
    83  		Result: &isolationSegment,
    84  	}
    85  
    86  	err = client.connection.Make(request, &response)
    87  	if err != nil {
    88  		return IsolationSegment{}, response.Warnings, err
    89  	}
    90  
    91  	return isolationSegment, response.Warnings, nil
    92  }
    93  
    94  // DeleteIsolationSegment removes an isolation segment from the cloud
    95  // controller. Note: This will only remove it from the cloud controller
    96  // database. It will not remove it from diego.
    97  func (client *Client) DeleteIsolationSegment(guid string) (Warnings, error) {
    98  	request, err := client.newHTTPRequest(requestOptions{
    99  		RequestName: internal.DeleteIsolationSegmentRequest,
   100  		URIParams:   map[string]string{"isolation_segment_guid": guid},
   101  	})
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	var response cloudcontroller.Response
   107  	err = client.connection.Make(request, &response)
   108  	return response.Warnings, err
   109  }