github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/nsxt_application_profile.go (about)

     1  /*
     2   * Copyright 2021 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  package govcd
     6  
     7  import (
     8  	"fmt"
     9  	"net/url"
    10  
    11  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    12  )
    13  
    14  // NsxtAppPortProfile uses OpenAPI endpoint to operate NSX-T Application Port Profiles
    15  // It can have 3 types of scopes:
    16  // * SYSTEM - Read-only (The ones that are provided by SYSTEM). Constant `types.ApplicationPortProfileScopeSystem`
    17  // * PROVIDER - Created by Provider on a particular network provider (NSX-T manager). Constant `types.ApplicationPortProfileScopeProvider`
    18  // * TENANT (Created by Tenant at Org VDC level). Constant `types.ApplicationPortProfileScopeTenant`
    19  //
    20  // More details about scope in documentation for types.NsxtAppPortProfile
    21  type NsxtAppPortProfile struct {
    22  	NsxtAppPortProfile *types.NsxtAppPortProfile
    23  	client             *Client
    24  }
    25  
    26  // CreateNsxtAppPortProfile allows users to create NSX-T Application Port Profile definition.
    27  // It can have 3 types of scopes:
    28  // * SYSTEM (The ones that are provided by SYSTEM) Read-only
    29  // * PROVIDER (Created by Provider globally)
    30  // * TENANT (Create by tenant at Org level)
    31  // More details about scope in documentation for types.NsxtAppPortProfile
    32  func (org *Org) CreateNsxtAppPortProfile(appPortProfileConfig *types.NsxtAppPortProfile) (*NsxtAppPortProfile, error) {
    33  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAppPortProfiles
    34  	minimumApiVersion, err := org.client.checkOpenApiEndpointCompatibility(endpoint)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	urlRef, err := org.client.OpenApiBuildEndpoint(endpoint)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	returnObject := &NsxtAppPortProfile{
    45  		NsxtAppPortProfile: &types.NsxtAppPortProfile{},
    46  		client:             org.client,
    47  	}
    48  
    49  	err = org.client.OpenApiPostItem(minimumApiVersion, urlRef, nil, appPortProfileConfig, returnObject.NsxtAppPortProfile, nil)
    50  	if err != nil {
    51  		return nil, fmt.Errorf("error creating NSX-T Application Port Profile: %s", err)
    52  	}
    53  
    54  	return returnObject, nil
    55  }
    56  
    57  // GetAllNsxtAppPortProfiles returns all NSX-T Application Port Profiles for specific scope
    58  // More details about scope in documentation for types.NsxtAppPortProfile
    59  func (org *Org) GetAllNsxtAppPortProfiles(queryParameters url.Values, scope string) ([]*NsxtAppPortProfile, error) {
    60  	queryParams := copyOrNewUrlValues(queryParameters)
    61  	if scope != "" {
    62  		queryParams = queryParameterFilterAnd("scope=="+scope, queryParams)
    63  	}
    64  
    65  	return getAllNsxtAppPortProfiles(org.client, queryParams)
    66  }
    67  
    68  // GetNsxtAppPortProfileByName allows users to retrieve Application Port Profiles for specific scope.
    69  // More details in documentation for types.NsxtAppPortProfile
    70  //
    71  // Note. Names are enforced to be unique per scope
    72  func (org *Org) GetNsxtAppPortProfileByName(name, scope string) (*NsxtAppPortProfile, error) {
    73  	queryParameters := url.Values{}
    74  	if scope != "" {
    75  		queryParameters = queryParameterFilterAnd("scope=="+scope, queryParameters)
    76  	}
    77  
    78  	return getNsxtAppPortProfileByName(org.client, name, queryParameters)
    79  }
    80  
    81  // GetNsxtAppPortProfileByName allows users to retrieve Application Port Profiles for specific scope.
    82  // More details in documentation for types.NsxtAppPortProfile
    83  //
    84  // Note. Names are enforced to be unique per scope
    85  func (vdc *Vdc) GetNsxtAppPortProfileByName(name, scope string) (*NsxtAppPortProfile, error) {
    86  	queryParameters := copyOrNewUrlValues(nil)
    87  	queryParameters = queryParameterFilterAnd("_context=="+vdc.Vdc.ID, queryParameters)
    88  	if scope != "" {
    89  		queryParameters = queryParameterFilterAnd("scope=="+scope, queryParameters)
    90  	}
    91  
    92  	return getNsxtAppPortProfileByName(vdc.client, name, queryParameters)
    93  }
    94  
    95  // GetNsxtAppPortProfileByName allows users to retrieve Application Port Profiles for specific scope.
    96  // More details in documentation for types.NsxtAppPortProfile
    97  //
    98  // Note. Names are enforced to be unique per scope
    99  func (vdcGroup *VdcGroup) GetNsxtAppPortProfileByName(name, scope string) (*NsxtAppPortProfile, error) {
   100  	queryParameters := copyOrNewUrlValues(nil)
   101  	queryParameters = queryParameterFilterAnd("_context=="+vdcGroup.VdcGroup.Id, queryParameters)
   102  
   103  	if scope != "" {
   104  		queryParameters = queryParameterFilterAnd("scope=="+scope, queryParameters)
   105  	}
   106  
   107  	return getNsxtAppPortProfileByName(vdcGroup.client, name, queryParameters)
   108  }
   109  
   110  // GetNsxtAppPortProfileById retrieves NSX-T Application Port Profile by ID
   111  func (org *Org) GetNsxtAppPortProfileById(id string) (*NsxtAppPortProfile, error) {
   112  	return getNsxtAppPortProfileById(org.client, id)
   113  }
   114  
   115  // Update allows users to update NSX-T Application Port Profile
   116  func (appPortProfile *NsxtAppPortProfile) Update(appPortProfileConfig *types.NsxtAppPortProfile) (*NsxtAppPortProfile, error) {
   117  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAppPortProfiles
   118  	minimumApiVersion, err := appPortProfile.client.checkOpenApiEndpointCompatibility(endpoint)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	if appPortProfileConfig.ID == "" {
   124  		return nil, fmt.Errorf("cannot update NSX-T Application Port Profile without ID")
   125  	}
   126  
   127  	urlRef, err := appPortProfile.client.OpenApiBuildEndpoint(endpoint, appPortProfileConfig.ID)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	returnObject := &NsxtAppPortProfile{
   133  		NsxtAppPortProfile: &types.NsxtAppPortProfile{},
   134  		client:             appPortProfile.client,
   135  	}
   136  
   137  	err = appPortProfile.client.OpenApiPutItem(minimumApiVersion, urlRef, nil, appPortProfileConfig, returnObject.NsxtAppPortProfile, nil)
   138  	if err != nil {
   139  		return nil, fmt.Errorf("error updating NSX-T Application Port Profile : %s", err)
   140  	}
   141  
   142  	return returnObject, nil
   143  }
   144  
   145  // Delete allows users to delete NSX-T Application Port Profile
   146  func (appPortProfile *NsxtAppPortProfile) Delete() error {
   147  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAppPortProfiles
   148  	minimumApiVersion, err := appPortProfile.client.checkOpenApiEndpointCompatibility(endpoint)
   149  	if err != nil {
   150  		return err
   151  	}
   152  
   153  	if appPortProfile.NsxtAppPortProfile.ID == "" {
   154  		return fmt.Errorf("cannot delete NSX-T Application Port Profile without ID")
   155  	}
   156  
   157  	urlRef, err := appPortProfile.client.OpenApiBuildEndpoint(endpoint, appPortProfile.NsxtAppPortProfile.ID)
   158  	if err != nil {
   159  		return err
   160  	}
   161  
   162  	err = appPortProfile.client.OpenApiDeleteItem(minimumApiVersion, urlRef, nil, nil)
   163  
   164  	if err != nil {
   165  		return fmt.Errorf("error deleting NSX-T Application Port Profile: %s", err)
   166  	}
   167  
   168  	return nil
   169  }
   170  
   171  func getNsxtAppPortProfileByName(client *Client, name string, queryParameters url.Values) (*NsxtAppPortProfile, error) {
   172  	queryParams := copyOrNewUrlValues(queryParameters)
   173  	queryParams = queryParameterFilterAnd("name=="+name, queryParams)
   174  
   175  	allAppPortProfiles, err := getAllNsxtAppPortProfiles(client, queryParams)
   176  	if err != nil {
   177  		return nil, fmt.Errorf("could not find NSX-T Application Port Profile with name '%s': %s", name, err)
   178  	}
   179  
   180  	if len(allAppPortProfiles) == 0 {
   181  		return nil, fmt.Errorf("%s: expected exactly one NSX-T Application Port Profile with name '%s'. Got %d", ErrorEntityNotFound, name, len(allAppPortProfiles))
   182  	}
   183  
   184  	if len(allAppPortProfiles) > 1 {
   185  		return nil, fmt.Errorf("expected exactly one NSX-T Application Port Profile with name '%s'. Got %d", name, len(allAppPortProfiles))
   186  	}
   187  
   188  	return getNsxtAppPortProfileById(client, allAppPortProfiles[0].NsxtAppPortProfile.ID)
   189  }
   190  
   191  func getNsxtAppPortProfileById(client *Client, id string) (*NsxtAppPortProfile, error) {
   192  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAppPortProfiles
   193  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	if id == "" {
   199  		return nil, fmt.Errorf("empty NSX-T Application Port Profile ID specified")
   200  	}
   201  
   202  	urlRef, err := client.OpenApiBuildEndpoint(endpoint, id)
   203  	if err != nil {
   204  		return nil, err
   205  	}
   206  
   207  	appPortProfile := &NsxtAppPortProfile{
   208  		NsxtAppPortProfile: &types.NsxtAppPortProfile{},
   209  		client:             client,
   210  	}
   211  
   212  	err = client.OpenApiGetItem(minimumApiVersion, urlRef, nil, appPortProfile.NsxtAppPortProfile, nil)
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	return appPortProfile, nil
   218  }
   219  
   220  func getAllNsxtAppPortProfiles(client *Client, queryParameters url.Values) ([]*NsxtAppPortProfile, error) {
   221  	endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAppPortProfiles
   222  	minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
   223  	if err != nil {
   224  		return nil, err
   225  	}
   226  
   227  	urlRef, err := client.OpenApiBuildEndpoint(endpoint)
   228  	if err != nil {
   229  		return nil, err
   230  	}
   231  
   232  	typeResponses := []*types.NsxtAppPortProfile{{}}
   233  	err = client.OpenApiGetAllItems(minimumApiVersion, urlRef, queryParameters, &typeResponses, nil)
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  
   238  	// Wrap all typeResponses into NsxtAppPortProfile types with client
   239  	wrappedResponses := make([]*NsxtAppPortProfile, len(typeResponses))
   240  	for sliceIndex := range typeResponses {
   241  		wrappedResponses[sliceIndex] = &NsxtAppPortProfile{
   242  			NsxtAppPortProfile: typeResponses[sliceIndex],
   243  			client:             client,
   244  		}
   245  	}
   246  
   247  	return wrappedResponses, nil
   248  }