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

     1  package govcd
     2  
     3  /*
     4   * Copyright 2020 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    11  )
    12  
    13  // QueryItem is an entity that is used to evaluate a Condition
    14  type QueryItem interface {
    15  	GetDate() string
    16  	GetName() string
    17  	GetType() string
    18  	GetIp() string
    19  	GetMetadataValue(key string) string
    20  	GetParentName() string
    21  	GetParentId() string
    22  	GetHref() string
    23  }
    24  
    25  type (
    26  	// All the Query* types are localizations of Query records that can be returned from a query.
    27  	// Each one of these implements the QueryItem interface
    28  	QueryVAppTemplate  types.QueryResultVappTemplateType
    29  	QueryCatalogItem   types.QueryResultCatalogItemType
    30  	QueryEdgeGateway   types.QueryResultEdgeGatewayRecordType
    31  	QueryAdminCatalog  types.AdminCatalogRecord
    32  	QueryCatalog       types.CatalogRecord
    33  	QueryOrgVdcNetwork types.QueryResultOrgVdcNetworkRecordType
    34  	QueryMedia         types.MediaRecordType
    35  	QueryVapp          types.QueryResultVAppRecordType
    36  	QueryVm            types.QueryResultVMRecordType
    37  	QueryOrgVdc        types.QueryResultOrgVdcRecordType
    38  	QueryTask          types.QueryResultTaskRecordType
    39  	QueryAdminTask     types.QueryResultTaskRecordType
    40  )
    41  
    42  // getMetadataValue is a generic metadata lookup for all query items
    43  func getMetadataValue(metadata *types.Metadata, key string) string {
    44  	if metadata == nil || len(metadata.MetadataEntry) == 0 {
    45  		return ""
    46  	}
    47  	for _, x := range metadata.MetadataEntry {
    48  		if key == x.Key {
    49  			return x.TypedValue.Value
    50  		}
    51  	}
    52  	return ""
    53  }
    54  
    55  // --------------------------------------------------------------
    56  // Org VDC
    57  // --------------------------------------------------------------
    58  func (orgVdc QueryOrgVdc) GetHref() string       { return orgVdc.HREF }
    59  func (orgVdc QueryOrgVdc) GetName() string       { return orgVdc.Name }
    60  func (orgVdc QueryOrgVdc) GetType() string       { return "org_vdc" }
    61  func (orgVdc QueryOrgVdc) GetIp() string         { return "" } // IP does not apply to VDC
    62  func (orgVdc QueryOrgVdc) GetDate() string       { return "" } // Date does not aply to VDC
    63  func (orgVdc QueryOrgVdc) GetParentName() string { return orgVdc.OrgName }
    64  func (orgVdc QueryOrgVdc) GetParentId() string   { return orgVdc.Org }
    65  func (orgVdc QueryOrgVdc) GetMetadataValue(key string) string {
    66  	return getMetadataValue(orgVdc.Metadata, key)
    67  }
    68  
    69  // --------------------------------------------------------------
    70  // vApp template
    71  // --------------------------------------------------------------
    72  func (vappTemplate QueryVAppTemplate) GetHref() string       { return vappTemplate.HREF }
    73  func (vappTemplate QueryVAppTemplate) GetName() string       { return vappTemplate.Name }
    74  func (vappTemplate QueryVAppTemplate) GetType() string       { return "vapp_template" }
    75  func (vappTemplate QueryVAppTemplate) GetIp() string         { return "" }
    76  func (vappTemplate QueryVAppTemplate) GetDate() string       { return vappTemplate.CreationDate }
    77  func (vappTemplate QueryVAppTemplate) GetParentName() string { return vappTemplate.CatalogName }
    78  func (vappTemplate QueryVAppTemplate) GetParentId() string   { return vappTemplate.Vdc }
    79  func (vappTemplate QueryVAppTemplate) GetMetadataValue(key string) string {
    80  	return getMetadataValue(vappTemplate.Metadata, key)
    81  }
    82  
    83  // --------------------------------------------------------------
    84  // media item
    85  // --------------------------------------------------------------
    86  func (media QueryMedia) GetHref() string       { return media.HREF }
    87  func (media QueryMedia) GetName() string       { return media.Name }
    88  func (media QueryMedia) GetType() string       { return "catalog_media" }
    89  func (media QueryMedia) GetIp() string         { return "" }
    90  func (media QueryMedia) GetDate() string       { return media.CreationDate }
    91  func (media QueryMedia) GetParentName() string { return media.CatalogName }
    92  func (media QueryMedia) GetParentId() string   { return media.Catalog }
    93  func (media QueryMedia) GetMetadataValue(key string) string {
    94  	return getMetadataValue(media.Metadata, key)
    95  }
    96  
    97  // --------------------------------------------------------------
    98  // catalog item
    99  // --------------------------------------------------------------
   100  func (catItem QueryCatalogItem) GetHref() string       { return catItem.HREF }
   101  func (catItem QueryCatalogItem) GetName() string       { return catItem.Name }
   102  func (catItem QueryCatalogItem) GetIp() string         { return "" }
   103  func (catItem QueryCatalogItem) GetType() string       { return "catalog_item" }
   104  func (catItem QueryCatalogItem) GetDate() string       { return catItem.CreationDate }
   105  func (catItem QueryCatalogItem) GetParentName() string { return catItem.CatalogName }
   106  func (catItem QueryCatalogItem) GetParentId() string   { return catItem.Catalog }
   107  func (catItem QueryCatalogItem) GetMetadataValue(key string) string {
   108  	return getMetadataValue(catItem.Metadata, key)
   109  }
   110  
   111  // --------------------------------------------------------------
   112  // catalog
   113  // --------------------------------------------------------------
   114  func (catalog QueryCatalog) GetHref() string       { return catalog.HREF }
   115  func (catalog QueryCatalog) GetName() string       { return catalog.Name }
   116  func (catalog QueryCatalog) GetIp() string         { return "" }
   117  func (catalog QueryCatalog) GetType() string       { return "catalog" }
   118  func (catalog QueryCatalog) GetDate() string       { return catalog.CreationDate }
   119  func (catalog QueryCatalog) GetParentName() string { return catalog.OrgName }
   120  func (catalog QueryCatalog) GetParentId() string   { return "" }
   121  func (catalog QueryCatalog) GetMetadataValue(key string) string {
   122  	return getMetadataValue(catalog.Metadata, key)
   123  }
   124  
   125  func (catalog QueryAdminCatalog) GetHref() string       { return catalog.HREF }
   126  func (catalog QueryAdminCatalog) GetName() string       { return catalog.Name }
   127  func (catalog QueryAdminCatalog) GetIp() string         { return "" }
   128  func (catalog QueryAdminCatalog) GetType() string       { return "catalog" }
   129  func (catalog QueryAdminCatalog) GetDate() string       { return catalog.CreationDate }
   130  func (catalog QueryAdminCatalog) GetParentName() string { return catalog.OrgName }
   131  func (catalog QueryAdminCatalog) GetParentId() string   { return "" }
   132  func (catalog QueryAdminCatalog) GetMetadataValue(key string) string {
   133  	return getMetadataValue(catalog.Metadata, key)
   134  }
   135  
   136  // --------------------------------------------------------------
   137  // edge gateway
   138  // --------------------------------------------------------------
   139  func (egw QueryEdgeGateway) GetHref() string       { return egw.HREF }
   140  func (egw QueryEdgeGateway) GetName() string       { return egw.Name }
   141  func (egw QueryEdgeGateway) GetIp() string         { return "" }
   142  func (egw QueryEdgeGateway) GetType() string       { return "edge_gateway" }
   143  func (egw QueryEdgeGateway) GetDate() string       { return "" }
   144  func (egw QueryEdgeGateway) GetParentName() string { return egw.OrgVdcName }
   145  func (egw QueryEdgeGateway) GetParentId() string   { return egw.Vdc }
   146  func (egw QueryEdgeGateway) GetMetadataValue(key string) string {
   147  	// Edge Gateway doesn't support metadata
   148  	return ""
   149  }
   150  
   151  // --------------------------------------------------------------
   152  // Org VDC network
   153  // --------------------------------------------------------------
   154  func (network QueryOrgVdcNetwork) GetHref() string { return network.HREF }
   155  func (network QueryOrgVdcNetwork) GetName() string { return network.Name }
   156  func (network QueryOrgVdcNetwork) GetIp() string   { return network.DefaultGateway }
   157  func (network QueryOrgVdcNetwork) GetType() string {
   158  	switch network.LinkType {
   159  	case 0:
   160  		return "network_direct"
   161  	case 1:
   162  		return "network_routed"
   163  	case 2:
   164  		return "network_isolated"
   165  	default:
   166  		// There are only three types so far, but just to make it future proof
   167  		return "network"
   168  	}
   169  }
   170  func (network QueryOrgVdcNetwork) GetDate() string       { return "" }
   171  func (network QueryOrgVdcNetwork) GetParentName() string { return network.VdcName }
   172  func (network QueryOrgVdcNetwork) GetParentId() string   { return network.Vdc }
   173  func (network QueryOrgVdcNetwork) GetMetadataValue(key string) string {
   174  	return getMetadataValue(network.Metadata, key)
   175  }
   176  
   177  // --------------------------------------------------------------
   178  // Task
   179  // --------------------------------------------------------------
   180  func (task QueryTask) GetHref() string       { return task.HREF }
   181  func (task QueryTask) GetName() string       { return task.Name }
   182  func (task QueryTask) GetType() string       { return "Task" }
   183  func (task QueryTask) GetIp() string         { return "" }
   184  func (task QueryTask) GetDate() string       { return task.StartDate }
   185  func (task QueryTask) GetParentName() string { return task.OwnerName }
   186  func (task QueryTask) GetParentId() string   { return task.Org }
   187  func (task QueryTask) GetMetadataValue(key string) string {
   188  	return getMetadataValue(task.Metadata, key)
   189  }
   190  
   191  // --------------------------------------------------------------
   192  // AdminTask
   193  // --------------------------------------------------------------
   194  func (task QueryAdminTask) GetHref() string       { return task.HREF }
   195  func (task QueryAdminTask) GetName() string       { return task.Name }
   196  func (task QueryAdminTask) GetType() string       { return "Task" }
   197  func (task QueryAdminTask) GetIp() string         { return "" }
   198  func (task QueryAdminTask) GetDate() string       { return task.StartDate }
   199  func (task QueryAdminTask) GetParentName() string { return task.OwnerName }
   200  func (task QueryAdminTask) GetParentId() string   { return task.Org }
   201  func (task QueryAdminTask) GetMetadataValue(key string) string {
   202  	return getMetadataValue(task.Metadata, key)
   203  }
   204  
   205  // --------------------------------------------------------------
   206  // vApp
   207  // --------------------------------------------------------------
   208  func (vapp QueryVapp) GetHref() string       { return vapp.HREF }
   209  func (vapp QueryVapp) GetName() string       { return vapp.Name }
   210  func (vapp QueryVapp) GetType() string       { return "vApp" }
   211  func (vapp QueryVapp) GetIp() string         { return "" }
   212  func (vapp QueryVapp) GetDate() string       { return vapp.CreationDate }
   213  func (vapp QueryVapp) GetParentName() string { return vapp.VdcName }
   214  func (vapp QueryVapp) GetParentId() string   { return vapp.VdcHREF }
   215  func (vapp QueryVapp) GetMetadataValue(key string) string {
   216  	return getMetadataValue(vapp.MetaData, key)
   217  }
   218  
   219  // --------------------------------------------------------------
   220  // VM
   221  // --------------------------------------------------------------
   222  func (vm QueryVm) GetHref() string       { return vm.HREF }
   223  func (vm QueryVm) GetName() string       { return vm.Name }
   224  func (vm QueryVm) GetType() string       { return "Vm" }
   225  func (vm QueryVm) GetIp() string         { return vm.IpAddress }
   226  func (vm QueryVm) GetDate() string       { return vm.DateCreated }
   227  func (vm QueryVm) GetParentName() string { return vm.ContainerName }
   228  func (vm QueryVm) GetParentId() string   { return vm.VdcHREF }
   229  func (vm QueryVm) GetMetadataValue(key string) string {
   230  	return getMetadataValue(vm.MetaData, key)
   231  }
   232  
   233  // --------------------------------------------------------------
   234  // result conversion
   235  // --------------------------------------------------------------
   236  // resultToQueryItems converts a set of query results into a list of query items
   237  func resultToQueryItems(queryType string, results Results) ([]QueryItem, error) {
   238  	resultSize := int64(results.Results.Total)
   239  	if resultSize < 1 {
   240  		return nil, nil
   241  	}
   242  	var items = make([]QueryItem, resultSize)
   243  	switch queryType {
   244  	case types.QtAdminCatalogItem:
   245  		for i, item := range results.Results.AdminCatalogItemRecord {
   246  			items[i] = QueryCatalogItem(*item)
   247  		}
   248  	case types.QtCatalogItem:
   249  		for i, item := range results.Results.CatalogItemRecord {
   250  			items[i] = QueryCatalogItem(*item)
   251  		}
   252  	case types.QtMedia:
   253  		for i, item := range results.Results.MediaRecord {
   254  			items[i] = QueryMedia(*item)
   255  		}
   256  	case types.QtAdminMedia:
   257  		for i, item := range results.Results.AdminMediaRecord {
   258  			items[i] = QueryMedia(*item)
   259  		}
   260  	case types.QtVappTemplate:
   261  		for i, item := range results.Results.VappTemplateRecord {
   262  			items[i] = QueryVAppTemplate(*item)
   263  		}
   264  	case types.QtAdminVappTemplate:
   265  		for i, item := range results.Results.AdminVappTemplateRecord {
   266  			items[i] = QueryVAppTemplate(*item)
   267  		}
   268  	case types.QtEdgeGateway:
   269  		for i, item := range results.Results.EdgeGatewayRecord {
   270  			items[i] = QueryEdgeGateway(*item)
   271  		}
   272  	case types.QtOrgVdcNetwork:
   273  		for i, item := range results.Results.OrgVdcNetworkRecord {
   274  			items[i] = QueryOrgVdcNetwork(*item)
   275  		}
   276  	case types.QtCatalog:
   277  		for i, item := range results.Results.CatalogRecord {
   278  			items[i] = QueryCatalog(*item)
   279  		}
   280  	case types.QtAdminCatalog:
   281  		for i, item := range results.Results.AdminCatalogRecord {
   282  			items[i] = QueryAdminCatalog(*item)
   283  		}
   284  	case types.QtVm:
   285  		for i, item := range results.Results.VMRecord {
   286  			items[i] = QueryVm(*item)
   287  		}
   288  	case types.QtAdminVm:
   289  		for i, item := range results.Results.AdminVMRecord {
   290  			items[i] = QueryVm(*item)
   291  		}
   292  	case types.QtVapp:
   293  		for i, item := range results.Results.VAppRecord {
   294  			items[i] = QueryVapp(*item)
   295  		}
   296  	case types.QtAdminVapp:
   297  		for i, item := range results.Results.AdminVAppRecord {
   298  			items[i] = QueryVapp(*item)
   299  		}
   300  	case types.QtOrgVdc:
   301  		for i, item := range results.Results.OrgVdcRecord {
   302  			items[i] = QueryOrgVdc(*item)
   303  		}
   304  	case types.QtAdminOrgVdc:
   305  		for i, item := range results.Results.OrgVdcAdminRecord {
   306  			items[i] = QueryOrgVdc(*item)
   307  		}
   308  	case types.QtTask:
   309  		for i, item := range results.Results.TaskRecord {
   310  			items[i] = QueryTask(*item)
   311  		}
   312  	case types.QtAdminTask:
   313  		for i, item := range results.Results.TaskRecord {
   314  			items[i] = QueryAdminTask(*item)
   315  		}
   316  
   317  	}
   318  	if len(items) > 0 {
   319  		return items, nil
   320  	}
   321  	return nil, fmt.Errorf("unsupported query type %s", queryType)
   322  }
   323  
   324  // GetQueryType is an utility function to get the appropriate query type depending on
   325  // the user's role
   326  func (client Client) GetQueryType(queryType string) string {
   327  	if client.IsSysAdmin {
   328  		adminType, ok := types.AdminQueryTypes[queryType]
   329  		if ok {
   330  			return adminType
   331  		} else {
   332  			panic(fmt.Sprintf("no corresponding admin type found for type %s", queryType))
   333  		}
   334  	}
   335  	return queryType
   336  }