From 0287d08b5e8555962e085b0fefde87fa6a8517f3 Mon Sep 17 00:00:00 2001 From: James Peru Date: Sat, 20 Jun 2026 13:06:31 +0300 Subject: [PATCH] Guard GetID helpers against count/empty-list panic (#103) The generated GetID and GetByID helpers index the result slice at [0] as soon as Count == 1, without checking the slice is non-empty. When the API reports count >= 1 but returns an empty list (a count/slice mismatch seen e.g. on the metrics APIs), this panics with 'index out of range [0] with length 0' (issue #103). Fix the generator template (generate/generate.go) to emit a length guard before the [0] access, returning the same 'No match found' error already used for the count == 0 case, and apply it to the generated helpers (additive only). Adds a regression test that panics on the unpatched code and passes with the guard. Signed-off-by: James Peru --- cloudstack/AccountService.go | 9 +++ cloudstack/AddressService.go | 3 + cloudstack/AffinityGroupService.go | 6 ++ cloudstack/AlertService.go | 6 ++ cloudstack/AnnotationService.go | 3 + cloudstack/AutoScaleService.go | 24 ++++++++ cloudstack/BGPPeerService.go | 3 + cloudstack/BackupService.go | 24 ++++++++ cloudstack/BrocadeVCSService.go | 3 + cloudstack/CertificateService.go | 3 + cloudstack/ClusterService.go | 15 +++++ cloudstack/ConfigurationService.go | 6 ++ cloudstack/DiskOfferingService.go | 6 ++ cloudstack/DomainService.go | 12 ++++ cloudstack/EventService.go | 3 + cloudstack/ExtensionService.go | 12 ++++ cloudstack/FirewallService.go | 15 +++++ cloudstack/GPUService.go | 15 +++++ cloudstack/GuestOSService.go | 15 +++++ cloudstack/HostService.go | 18 ++++++ cloudstack/HypervisorService.go | 3 + cloudstack/ISOService.go | 9 +++ cloudstack/ImageStoreService.go | 15 +++++ cloudstack/InternalLBService.go | 9 +++ cloudstack/KubernetesService.go | 12 ++++ cloudstack/LDAPService.go | 3 + cloudstack/LoadBalancerService.go | 30 ++++++++++ cloudstack/ManagementService.go | 12 ++++ cloudstack/NATService.go | 3 + cloudstack/NetscalerService.go | 3 + cloudstack/NetworkACLService.go | 9 +++ cloudstack/NetworkOfferingService.go | 6 ++ cloudstack/NetworkService.go | 33 +++++++++++ cloudstack/OauthService.go | 6 ++ cloudstack/ObjectStoreService.go | 6 ++ cloudstack/OvsElementService.go | 3 + cloudstack/PodService.go | 6 ++ cloudstack/PoolService.go | 6 ++ cloudstack/PortableIPService.go | 3 + cloudstack/ProjectService.go | 9 +++ cloudstack/ResourcetagsService.go | 3 + cloudstack/RoleService.go | 9 +++ cloudstack/RouterService.go | 9 +++ cloudstack/SSHService.go | 6 ++ cloudstack/SecurityGroupService.go | 6 ++ cloudstack/ServiceOfferingService.go | 6 ++ cloudstack/SharedFileSystemService.go | 6 ++ cloudstack/SnapshotService.go | 12 ++++ cloudstack/StoragePoolService.go | 18 ++++++ cloudstack/SwiftService.go | 3 + cloudstack/SystemVMService.go | 12 ++++ cloudstack/TemplateService.go | 9 +++ cloudstack/UCSService.go | 6 ++ cloudstack/UsageService.go | 3 + cloudstack/UserService.go | 9 +++ cloudstack/VLANService.go | 6 ++ cloudstack/VMGroupService.go | 6 ++ cloudstack/VPCService.go | 18 ++++++ cloudstack/VPNService.go | 18 ++++++ cloudstack/VirtualMachineService.go | 21 +++++++ cloudstack/VirtualNetworkFunctionsService.go | 12 ++++ cloudstack/VolumeService.go | 21 +++++++ cloudstack/WebhookService.go | 12 ++++ cloudstack/ZoneService.go | 18 ++++++ generate/generate.go | 6 ++ test/GetMetricIDRegression_test.go | 58 ++++++++++++++++++++ 66 files changed, 700 insertions(+) create mode 100644 test/GetMetricIDRegression_test.go diff --git a/cloudstack/AccountService.go b/cloudstack/AccountService.go index be336787..f7183bc0 100644 --- a/cloudstack/AccountService.go +++ b/cloudstack/AccountService.go @@ -1804,6 +1804,9 @@ func (s *AccountService) GetAccountID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.Accounts) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Accounts[0].Id, l.Count, nil } @@ -1859,6 +1862,9 @@ func (s *AccountService) GetAccountByID(id string, opts ...OptionFunc) (*Account } if l.Count == 1 { + if len(l.Accounts) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Accounts[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Account UUID: %s!", id) @@ -2232,6 +2238,9 @@ func (s *AccountService) GetProjectAccountID(keyword string, projectid string, o } if l.Count == 1 { + if len(l.ProjectAccounts) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.ProjectAccounts[0].Id, l.Count, nil } diff --git a/cloudstack/AddressService.go b/cloudstack/AddressService.go index 98ce3aa8..b08690d8 100644 --- a/cloudstack/AddressService.go +++ b/cloudstack/AddressService.go @@ -1303,6 +1303,9 @@ func (s *AddressService) GetPublicIpAddressByID(id string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.PublicIpAddresses) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.PublicIpAddresses[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for PublicIpAddress UUID: %s!", id) diff --git a/cloudstack/AffinityGroupService.go b/cloudstack/AffinityGroupService.go index afd1767e..1851c143 100644 --- a/cloudstack/AffinityGroupService.go +++ b/cloudstack/AffinityGroupService.go @@ -897,6 +897,9 @@ func (s *AffinityGroupService) GetAffinityGroupID(name string, opts ...OptionFun } if l.Count == 1 { + if len(l.AffinityGroups) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.AffinityGroups[0].Id, l.Count, nil } @@ -956,6 +959,9 @@ func (s *AffinityGroupService) GetAffinityGroupByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.AffinityGroups) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.AffinityGroups[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for AffinityGroup UUID: %s!", id) diff --git a/cloudstack/AlertService.go b/cloudstack/AlertService.go index a2a0b208..649cf720 100644 --- a/cloudstack/AlertService.go +++ b/cloudstack/AlertService.go @@ -745,6 +745,9 @@ func (s *AlertService) GetAlertID(name string, opts ...OptionFunc) (string, int, } if l.Count == 1 { + if len(l.Alerts) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Alerts[0].Id, l.Count, nil } @@ -800,6 +803,9 @@ func (s *AlertService) GetAlertByID(id string, opts ...OptionFunc) (*Alert, int, } if l.Count == 1 { + if len(l.Alerts) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Alerts[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Alert UUID: %s!", id) diff --git a/cloudstack/AnnotationService.go b/cloudstack/AnnotationService.go index 584883e0..45875773 100644 --- a/cloudstack/AnnotationService.go +++ b/cloudstack/AnnotationService.go @@ -432,6 +432,9 @@ func (s *AnnotationService) GetAnnotationByID(id string, opts ...OptionFunc) (*A } if l.Count == 1 { + if len(l.Annotations) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Annotations[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Annotation UUID: %s!", id) diff --git a/cloudstack/AutoScaleService.go b/cloudstack/AutoScaleService.go index 58aab954..45dfb7e2 100644 --- a/cloudstack/AutoScaleService.go +++ b/cloudstack/AutoScaleService.go @@ -2432,6 +2432,9 @@ func (s *AutoScaleService) GetAutoScalePolicyID(name string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.AutoScalePolicies) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.AutoScalePolicies[0].Id, l.Count, nil } @@ -2487,6 +2490,9 @@ func (s *AutoScaleService) GetAutoScalePolicyByID(id string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.AutoScalePolicies) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.AutoScalePolicies[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for AutoScalePolicy UUID: %s!", id) @@ -2937,6 +2943,9 @@ func (s *AutoScaleService) GetAutoScaleVmGroupID(name string, opts ...OptionFunc } if l.Count == 1 { + if len(l.AutoScaleVmGroups) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.AutoScaleVmGroups[0].Id, l.Count, nil } @@ -2992,6 +3001,9 @@ func (s *AutoScaleService) GetAutoScaleVmGroupByID(id string, opts ...OptionFunc } if l.Count == 1 { + if len(l.AutoScaleVmGroups) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.AutoScaleVmGroups[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for AutoScaleVmGroup UUID: %s!", id) @@ -3438,6 +3450,9 @@ func (s *AutoScaleService) GetAutoScaleVmProfileByID(id string, opts ...OptionFu } if l.Count == 1 { + if len(l.AutoScaleVmProfiles) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.AutoScaleVmProfiles[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for AutoScaleVmProfile UUID: %s!", id) @@ -3803,6 +3818,9 @@ func (s *AutoScaleService) GetConditionByID(id string, opts ...OptionFunc) (*Con } if l.Count == 1 { + if len(l.Conditions) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Conditions[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Condition UUID: %s!", id) @@ -4059,6 +4077,9 @@ func (s *AutoScaleService) GetCounterID(name string, opts ...OptionFunc) (string } if l.Count == 1 { + if len(l.Counters) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Counters[0].Id, l.Count, nil } @@ -4114,6 +4135,9 @@ func (s *AutoScaleService) GetCounterByID(id string, opts ...OptionFunc) (*Count } if l.Count == 1 { + if len(l.Counters) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Counters[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Counter UUID: %s!", id) diff --git a/cloudstack/BGPPeerService.go b/cloudstack/BGPPeerService.go index c71a5584..40750273 100644 --- a/cloudstack/BGPPeerService.go +++ b/cloudstack/BGPPeerService.go @@ -1010,6 +1010,9 @@ func (s *BGPPeerService) GetBgpPeerByID(id string, opts ...OptionFunc) (*BgpPeer } if l.Count == 1 { + if len(l.BgpPeers) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.BgpPeers[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for BgpPeer UUID: %s!", id) diff --git a/cloudstack/BackupService.go b/cloudstack/BackupService.go index 92033d83..2c00dd7b 100644 --- a/cloudstack/BackupService.go +++ b/cloudstack/BackupService.go @@ -3151,6 +3151,9 @@ func (s *BackupService) GetBackupOfferingID(keyword string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.BackupOfferings) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.BackupOfferings[0].Id, l.Count, nil } @@ -3206,6 +3209,9 @@ func (s *BackupService) GetBackupOfferingByID(id string, opts ...OptionFunc) (*B } if l.Count == 1 { + if len(l.BackupOfferings) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.BackupOfferings[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for BackupOffering UUID: %s!", id) @@ -3389,6 +3395,9 @@ func (s *BackupService) GetBackupProviderOfferingID(keyword string, zoneid strin } if l.Count == 1 { + if len(l.BackupProviderOfferings) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.BackupProviderOfferings[0].Id, l.Count, nil } @@ -3721,6 +3730,9 @@ func (s *BackupService) GetBackupRepositoryID(name string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.BackupRepositories) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.BackupRepositories[0].Id, l.Count, nil } @@ -3776,6 +3788,9 @@ func (s *BackupService) GetBackupRepositoryByID(id string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.BackupRepositories) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.BackupRepositories[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for BackupRepository UUID: %s!", id) @@ -4108,6 +4123,9 @@ func (s *BackupService) GetBackupScheduleByID(id string, opts ...OptionFunc) (*B } if l.Count == 1 { + if len(l.BackupSchedule) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.BackupSchedule[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for BackupSchedule UUID: %s!", id) @@ -4530,6 +4548,9 @@ func (s *BackupService) GetBackupID(name string, opts ...OptionFunc) (string, in } if l.Count == 1 { + if len(l.Backups) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Backups[0].Id, l.Count, nil } @@ -4585,6 +4606,9 @@ func (s *BackupService) GetBackupByID(id string, opts ...OptionFunc) (*Backup, i } if l.Count == 1 { + if len(l.Backups) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Backups[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Backup UUID: %s!", id) diff --git a/cloudstack/BrocadeVCSService.go b/cloudstack/BrocadeVCSService.go index 571b3433..76fe89bd 100644 --- a/cloudstack/BrocadeVCSService.go +++ b/cloudstack/BrocadeVCSService.go @@ -428,6 +428,9 @@ func (s *BrocadeVCSService) GetBrocadeVcsDeviceNetworkID(keyword string, vcsdevi } if l.Count == 1 { + if len(l.BrocadeVcsDeviceNetworks) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.BrocadeVcsDeviceNetworks[0].Id, l.Count, nil } diff --git a/cloudstack/CertificateService.go b/cloudstack/CertificateService.go index e2aa4027..16ae2f80 100644 --- a/cloudstack/CertificateService.go +++ b/cloudstack/CertificateService.go @@ -573,6 +573,9 @@ func (s *CertificateService) GetTemplateDirectDownloadCertificateByID(id string, } if l.Count == 1 { + if len(l.TemplateDirectDownloadCertificates) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.TemplateDirectDownloadCertificates[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for TemplateDirectDownloadCertificate UUID: %s!", id) diff --git a/cloudstack/ClusterService.go b/cloudstack/ClusterService.go index 1e10470b..b8afd426 100644 --- a/cloudstack/ClusterService.go +++ b/cloudstack/ClusterService.go @@ -1898,6 +1898,9 @@ func (s *ClusterService) GetClusterID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.Clusters) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Clusters[0].Id, l.Count, nil } @@ -1953,6 +1956,9 @@ func (s *ClusterService) GetClusterByID(id string, opts ...OptionFunc) (*Cluster } if l.Count == 1 { + if len(l.Clusters) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Clusters[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Cluster UUID: %s!", id) @@ -2191,6 +2197,9 @@ func (s *ClusterService) GetClusterDrsPlanByID(id string, opts ...OptionFunc) (* } if l.Count == 1 { + if len(l.ClusterDrsPlan) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ClusterDrsPlan[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ClusterDrsPlan UUID: %s!", id) @@ -2609,6 +2618,9 @@ func (s *ClusterService) GetClustersMetricID(name string, opts ...OptionFunc) (s } if l.Count == 1 { + if len(l.ClustersMetrics) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ClustersMetrics[0].Id, l.Count, nil } @@ -2664,6 +2676,9 @@ func (s *ClusterService) GetClustersMetricByID(id string, opts ...OptionFunc) (* } if l.Count == 1 { + if len(l.ClustersMetrics) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ClustersMetrics[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ClustersMetric UUID: %s!", id) diff --git a/cloudstack/ConfigurationService.go b/cloudstack/ConfigurationService.go index 571b85cf..f75b00d3 100644 --- a/cloudstack/ConfigurationService.go +++ b/cloudstack/ConfigurationService.go @@ -1879,6 +1879,9 @@ func (s *ConfigurationService) GetCniConfigurationID(name string, opts ...Option } if l.Count == 1 { + if len(l.CniConfiguration) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.CniConfiguration[0].Id, l.Count, nil } @@ -1934,6 +1937,9 @@ func (s *ConfigurationService) GetCniConfigurationByID(id string, opts ...Option } if l.Count == 1 { + if len(l.CniConfiguration) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.CniConfiguration[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for CniConfiguration UUID: %s!", id) diff --git a/cloudstack/DiskOfferingService.go b/cloudstack/DiskOfferingService.go index a2e6cb00..93f4b1a5 100644 --- a/cloudstack/DiskOfferingService.go +++ b/cloudstack/DiskOfferingService.go @@ -1445,6 +1445,9 @@ func (s *DiskOfferingService) GetDiskOfferingID(name string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.DiskOfferings) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.DiskOfferings[0].Id, l.Count, nil } @@ -1500,6 +1503,9 @@ func (s *DiskOfferingService) GetDiskOfferingByID(id string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.DiskOfferings) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.DiskOfferings[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for DiskOffering UUID: %s!", id) diff --git a/cloudstack/DomainService.go b/cloudstack/DomainService.go index 473a2195..a6efb65d 100644 --- a/cloudstack/DomainService.go +++ b/cloudstack/DomainService.go @@ -601,6 +601,9 @@ func (s *DomainService) GetDomainChildrenID(name string, opts ...OptionFunc) (st } if l.Count == 1 { + if len(l.DomainChildren) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.DomainChildren[0].Id, l.Count, nil } @@ -656,6 +659,9 @@ func (s *DomainService) GetDomainChildrenByID(id string, opts ...OptionFunc) (*D } if l.Count == 1 { + if len(l.DomainChildren) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.DomainChildren[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for DomainChildren UUID: %s!", id) @@ -1040,6 +1046,9 @@ func (s *DomainService) GetDomainID(name string, opts ...OptionFunc) (string, in } if l.Count == 1 { + if len(l.Domains) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Domains[0].Id, l.Count, nil } @@ -1095,6 +1104,9 @@ func (s *DomainService) GetDomainByID(id string, opts ...OptionFunc) (*Domain, i } if l.Count == 1 { + if len(l.Domains) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Domains[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Domain UUID: %s!", id) diff --git a/cloudstack/EventService.go b/cloudstack/EventService.go index bdf83652..ce6627d4 100644 --- a/cloudstack/EventService.go +++ b/cloudstack/EventService.go @@ -952,6 +952,9 @@ func (s *EventService) GetEventByID(id string, opts ...OptionFunc) (*Event, int, } if l.Count == 1 { + if len(l.Events) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Events[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Event UUID: %s!", id) diff --git a/cloudstack/ExtensionService.go b/cloudstack/ExtensionService.go index b565f4a0..f7c237f0 100644 --- a/cloudstack/ExtensionService.go +++ b/cloudstack/ExtensionService.go @@ -1094,6 +1094,9 @@ func (s *ExtensionService) GetCustomActionID(name string, opts ...OptionFunc) (s } if l.Count == 1 { + if len(l.CustomActions) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.CustomActions[0].Id, l.Count, nil } @@ -1149,6 +1152,9 @@ func (s *ExtensionService) GetCustomActionByID(id string, opts ...OptionFunc) (* } if l.Count == 1 { + if len(l.CustomActions) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.CustomActions[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for CustomAction UUID: %s!", id) @@ -1391,6 +1397,9 @@ func (s *ExtensionService) GetExtensionID(name string, opts ...OptionFunc) (stri } if l.Count == 1 { + if len(l.Extensions) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Extensions[0].Id, l.Count, nil } @@ -1446,6 +1455,9 @@ func (s *ExtensionService) GetExtensionByID(id string, opts ...OptionFunc) (*Ext } if l.Count == 1 { + if len(l.Extensions) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Extensions[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Extension UUID: %s!", id) diff --git a/cloudstack/FirewallService.go b/cloudstack/FirewallService.go index c299de5e..0ff3c79b 100644 --- a/cloudstack/FirewallService.go +++ b/cloudstack/FirewallService.go @@ -2620,6 +2620,9 @@ func (s *FirewallService) GetEgressFirewallRuleByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.EgressFirewallRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.EgressFirewallRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for EgressFirewallRule UUID: %s!", id) @@ -3039,6 +3042,9 @@ func (s *FirewallService) GetFirewallRuleByID(id string, opts ...OptionFunc) (*F } if l.Count == 1 { + if len(l.FirewallRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.FirewallRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for FirewallRule UUID: %s!", id) @@ -3646,6 +3652,9 @@ func (s *FirewallService) GetPortForwardingRuleByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.PortForwardingRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.PortForwardingRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for PortForwardingRule UUID: %s!", id) @@ -4067,6 +4076,9 @@ func (s *FirewallService) GetRoutingFirewallRuleByID(id string, opts ...OptionFu } if l.Count == 1 { + if len(l.RoutingFirewallRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.RoutingFirewallRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for RoutingFirewallRule UUID: %s!", id) @@ -5075,6 +5087,9 @@ func (s *FirewallService) GetIpv6FirewallRuleByID(id string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.Ipv6FirewallRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Ipv6FirewallRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Ipv6FirewallRule UUID: %s!", id) diff --git a/cloudstack/GPUService.go b/cloudstack/GPUService.go index 753f5cc6..1dc16061 100644 --- a/cloudstack/GPUService.go +++ b/cloudstack/GPUService.go @@ -1444,6 +1444,9 @@ func (s *GPUService) GetGpuCardID(keyword string, opts ...OptionFunc) (string, i } if l.Count == 1 { + if len(l.GpuCards) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.GpuCards[0].Id, l.Count, nil } @@ -1499,6 +1502,9 @@ func (s *GPUService) GetGpuCardByID(id string, opts ...OptionFunc) (*GpuCard, in } if l.Count == 1 { + if len(l.GpuCards) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.GpuCards[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for GpuCard UUID: %s!", id) @@ -1777,6 +1783,9 @@ func (s *GPUService) GetGpuDeviceByID(id string, opts ...OptionFunc) (*GpuDevice } if l.Count == 1 { + if len(l.GpuDevices) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.GpuDevices[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for GpuDevice UUID: %s!", id) @@ -2037,6 +2046,9 @@ func (s *GPUService) GetVgpuProfileID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.VgpuProfiles) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VgpuProfiles[0].Id, l.Count, nil } @@ -2092,6 +2104,9 @@ func (s *GPUService) GetVgpuProfileByID(id string, opts ...OptionFunc) (*VgpuPro } if l.Count == 1 { + if len(l.VgpuProfiles) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VgpuProfiles[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VgpuProfile UUID: %s!", id) diff --git a/cloudstack/GuestOSService.go b/cloudstack/GuestOSService.go index f7dd0c03..ac64ef15 100644 --- a/cloudstack/GuestOSService.go +++ b/cloudstack/GuestOSService.go @@ -784,6 +784,9 @@ func (s *GuestOSService) GetGuestOsMappingByID(id string, opts ...OptionFunc) (* } if l.Count == 1 { + if len(l.GuestOsMapping) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.GuestOsMapping[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for GuestOsMapping UUID: %s!", id) @@ -1161,6 +1164,9 @@ func (s *GuestOSService) GetOsCategoryID(name string, opts ...OptionFunc) (strin } if l.Count == 1 { + if len(l.OsCategories) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.OsCategories[0].Id, l.Count, nil } @@ -1216,6 +1222,9 @@ func (s *GuestOSService) GetOsCategoryByID(id string, opts ...OptionFunc) (*OsCa } if l.Count == 1 { + if len(l.OsCategories) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.OsCategories[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for OsCategory UUID: %s!", id) @@ -1465,6 +1474,9 @@ func (s *GuestOSService) GetOsTypeID(keyword string, opts ...OptionFunc) (string } if l.Count == 1 { + if len(l.OsTypes) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.OsTypes[0].Id, l.Count, nil } @@ -1520,6 +1532,9 @@ func (s *GuestOSService) GetOsTypeByID(id string, opts ...OptionFunc) (*OsType, } if l.Count == 1 { + if len(l.OsTypes) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.OsTypes[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for OsType UUID: %s!", id) diff --git a/cloudstack/HostService.go b/cloudstack/HostService.go index 16f9009b..e0b33018 100644 --- a/cloudstack/HostService.go +++ b/cloudstack/HostService.go @@ -2767,6 +2767,9 @@ func (s *HostService) GetHostTagID(keyword string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.HostTags) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.HostTags[0].Id, l.Count, nil } @@ -3337,6 +3340,9 @@ func (s *HostService) GetHostID(name string, opts ...OptionFunc) (string, int, e } if l.Count == 1 { + if len(l.Hosts) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Hosts[0].Id, l.Count, nil } @@ -3392,6 +3398,9 @@ func (s *HostService) GetHostByID(id string, opts ...OptionFunc) (*Host, int, er } if l.Count == 1 { + if len(l.Hosts) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Hosts[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Host UUID: %s!", id) @@ -4041,6 +4050,9 @@ func (s *HostService) GetHostsMetricID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.HostsMetrics) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.HostsMetrics[0].Id, l.Count, nil } @@ -4096,6 +4108,9 @@ func (s *HostService) GetHostsMetricByID(id string, opts ...OptionFunc) (*HostsM } if l.Count == 1 { + if len(l.HostsMetrics) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.HostsMetrics[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for HostsMetric UUID: %s!", id) @@ -5904,6 +5919,9 @@ func (s *HostService) GetSecondaryStorageSelectorID(keyword string, zoneid strin } if l.Count == 1 { + if len(l.SecondaryStorageSelectors) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.SecondaryStorageSelectors[0].Id, l.Count, nil } diff --git a/cloudstack/HypervisorService.go b/cloudstack/HypervisorService.go index 605070ed..747adc73 100644 --- a/cloudstack/HypervisorService.go +++ b/cloudstack/HypervisorService.go @@ -207,6 +207,9 @@ func (s *HypervisorService) GetHypervisorCapabilityByID(id string, opts ...Optio } if l.Count == 1 { + if len(l.HypervisorCapabilities) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.HypervisorCapabilities[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for HypervisorCapability UUID: %s!", id) diff --git a/cloudstack/ISOService.go b/cloudstack/ISOService.go index 5000bc5f..31ec4ba6 100644 --- a/cloudstack/ISOService.go +++ b/cloudstack/ISOService.go @@ -1610,6 +1610,9 @@ func (s *ISOService) GetIsoPermissionByID(id string, opts ...OptionFunc) (*IsoPe } if l.Count == 1 { + if len(l.IsoPermissions) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.IsoPermissions[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for IsoPermission UUID: %s!", id) @@ -2280,6 +2283,9 @@ func (s *ISOService) GetIsoID(name string, isofilter string, zoneid string, opts } if l.Count == 1 { + if len(l.Isos) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Isos[0].Id, l.Count, nil } @@ -2335,6 +2341,9 @@ func (s *ISOService) GetIsoByID(id string, opts ...OptionFunc) (*Iso, int, error } if l.Count == 1 { + if len(l.Isos) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Isos[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Iso UUID: %s!", id) diff --git a/cloudstack/ImageStoreService.go b/cloudstack/ImageStoreService.go index 8ff7f9bd..0adb549f 100644 --- a/cloudstack/ImageStoreService.go +++ b/cloudstack/ImageStoreService.go @@ -1195,6 +1195,9 @@ func (s *ImageStoreService) GetImageStoreID(name string, opts ...OptionFunc) (st } if l.Count == 1 { + if len(l.ImageStores) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ImageStores[0].Id, l.Count, nil } @@ -1250,6 +1253,9 @@ func (s *ImageStoreService) GetImageStoreByID(id string, opts ...OptionFunc) (*I } if l.Count == 1 { + if len(l.ImageStores) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ImageStores[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ImageStore UUID: %s!", id) @@ -1529,6 +1535,9 @@ func (s *ImageStoreService) GetSecondaryStagingStoreID(name string, opts ...Opti } if l.Count == 1 { + if len(l.SecondaryStagingStores) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.SecondaryStagingStores[0].Id, l.Count, nil } @@ -1584,6 +1593,9 @@ func (s *ImageStoreService) GetSecondaryStagingStoreByID(id string, opts ...Opti } if l.Count == 1 { + if len(l.SecondaryStagingStores) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.SecondaryStagingStores[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for SecondaryStagingStore UUID: %s!", id) @@ -2113,6 +2125,9 @@ func (s *ImageStoreService) GetImageStoreObjectByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.ImageStoreObjects) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ImageStoreObjects[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ImageStoreObject UUID: %s!", id) diff --git a/cloudstack/InternalLBService.go b/cloudstack/InternalLBService.go index 1e2c53ce..adf90d5c 100644 --- a/cloudstack/InternalLBService.go +++ b/cloudstack/InternalLBService.go @@ -443,6 +443,9 @@ func (s *InternalLBService) GetInternalLoadBalancerElementByID(id string, opts . } if l.Count == 1 { + if len(l.InternalLoadBalancerElements) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.InternalLoadBalancerElements[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for InternalLoadBalancerElement UUID: %s!", id) @@ -957,6 +960,9 @@ func (s *InternalLBService) GetInternalLoadBalancerVMID(name string, opts ...Opt } if l.Count == 1 { + if len(l.InternalLoadBalancerVMs) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.InternalLoadBalancerVMs[0].Id, l.Count, nil } @@ -1012,6 +1018,9 @@ func (s *InternalLBService) GetInternalLoadBalancerVMByID(id string, opts ...Opt } if l.Count == 1 { + if len(l.InternalLoadBalancerVMs) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.InternalLoadBalancerVMs[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for InternalLoadBalancerVM UUID: %s!", id) diff --git a/cloudstack/KubernetesService.go b/cloudstack/KubernetesService.go index 0ce0afb0..16ea5a44 100644 --- a/cloudstack/KubernetesService.go +++ b/cloudstack/KubernetesService.go @@ -1749,6 +1749,9 @@ func (s *KubernetesService) GetKubernetesClusterID(name string, opts ...OptionFu } if l.Count == 1 { + if len(l.KubernetesClusters) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.KubernetesClusters[0].Id, l.Count, nil } @@ -1804,6 +1807,9 @@ func (s *KubernetesService) GetKubernetesClusterByID(id string, opts ...OptionFu } if l.Count == 1 { + if len(l.KubernetesClusters) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.KubernetesClusters[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for KubernetesCluster UUID: %s!", id) @@ -2119,6 +2125,9 @@ func (s *KubernetesService) GetKubernetesSupportedVersionID(keyword string, opts } if l.Count == 1 { + if len(l.KubernetesSupportedVersions) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.KubernetesSupportedVersions[0].Id, l.Count, nil } @@ -2174,6 +2183,9 @@ func (s *KubernetesService) GetKubernetesSupportedVersionByID(id string, opts .. } if l.Count == 1 { + if len(l.KubernetesSupportedVersions) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.KubernetesSupportedVersions[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for KubernetesSupportedVersion UUID: %s!", id) diff --git a/cloudstack/LDAPService.go b/cloudstack/LDAPService.go index b2ab0625..2b854834 100644 --- a/cloudstack/LDAPService.go +++ b/cloudstack/LDAPService.go @@ -1770,6 +1770,9 @@ func (s *LDAPService) GetLdapConfigurationByID(id string, opts ...OptionFunc) (* } if l.Count == 1 { + if len(l.LdapConfigurations) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.LdapConfigurations[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for LdapConfiguration UUID: %s!", id) diff --git a/cloudstack/LoadBalancerService.go b/cloudstack/LoadBalancerService.go index c66a8c0c..3fbd2cbe 100644 --- a/cloudstack/LoadBalancerService.go +++ b/cloudstack/LoadBalancerService.go @@ -3197,6 +3197,9 @@ func (s *LoadBalancerService) GetGlobalLoadBalancerRuleID(keyword string, opts . } if l.Count == 1 { + if len(l.GlobalLoadBalancerRules) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.GlobalLoadBalancerRules[0].Id, l.Count, nil } @@ -3252,6 +3255,9 @@ func (s *LoadBalancerService) GetGlobalLoadBalancerRuleByID(id string, opts ...O } if l.Count == 1 { + if len(l.GlobalLoadBalancerRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.GlobalLoadBalancerRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for GlobalLoadBalancerRule UUID: %s!", id) @@ -3517,6 +3523,9 @@ func (s *LoadBalancerService) GetLBHealthCheckPolicyByID(id string, opts ...Opti } if l.Count == 1 { + if len(l.LBHealthCheckPolicies) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.LBHealthCheckPolicies[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for LBHealthCheckPolicy UUID: %s!", id) @@ -3760,6 +3769,9 @@ func (s *LoadBalancerService) GetLBStickinessPolicyByID(id string, opts ...Optio } if l.Count == 1 { + if len(l.LBStickinessPolicies) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.LBStickinessPolicies[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for LBStickinessPolicy UUID: %s!", id) @@ -4006,6 +4018,9 @@ func (s *LoadBalancerService) GetLoadBalancerRuleInstanceByID(id string, opts .. } if l.Count == 1 { + if len(l.LoadBalancerRuleInstances) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.LoadBalancerRuleInstances[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for LoadBalancerRuleInstance UUID: %s!", id) @@ -4475,6 +4490,9 @@ func (s *LoadBalancerService) GetLoadBalancerRuleID(name string, opts ...OptionF } if l.Count == 1 { + if len(l.LoadBalancerRules) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.LoadBalancerRules[0].Id, l.Count, nil } @@ -4530,6 +4548,9 @@ func (s *LoadBalancerService) GetLoadBalancerRuleByID(id string, opts ...OptionF } if l.Count == 1 { + if len(l.LoadBalancerRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.LoadBalancerRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for LoadBalancerRule UUID: %s!", id) @@ -5018,6 +5039,9 @@ func (s *LoadBalancerService) GetLoadBalancerID(name string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.LoadBalancers) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.LoadBalancers[0].Id, l.Count, nil } @@ -5073,6 +5097,9 @@ func (s *LoadBalancerService) GetLoadBalancerByID(id string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.LoadBalancers) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.LoadBalancers[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for LoadBalancer UUID: %s!", id) @@ -5250,6 +5277,9 @@ func (s *LoadBalancerService) GetRegisteredServicePackageID(keyword string, opts } if l.Count == 1 { + if len(l.RegisteredServicePackages) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.RegisteredServicePackages[0].Id, l.Count, nil } diff --git a/cloudstack/ManagementService.go b/cloudstack/ManagementService.go index a8370f28..3f37476a 100644 --- a/cloudstack/ManagementService.go +++ b/cloudstack/ManagementService.go @@ -311,6 +311,9 @@ func (s *ManagementService) GetManagementServerID(name string, opts ...OptionFun } if l.Count == 1 { + if len(l.ManagementServers) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ManagementServers[0].Id, l.Count, nil } @@ -366,6 +369,9 @@ func (s *ManagementService) GetManagementServerByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.ManagementServers) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ManagementServers[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ManagementServer UUID: %s!", id) @@ -629,6 +635,9 @@ func (s *ManagementService) GetManagementServersMetricID(name string, opts ...Op } if l.Count == 1 { + if len(l.ManagementServersMetrics) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ManagementServersMetrics[0].Id, l.Count, nil } @@ -684,6 +693,9 @@ func (s *ManagementService) GetManagementServersMetricByID(id string, opts ...Op } if l.Count == 1 { + if len(l.ManagementServersMetrics) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ManagementServersMetrics[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ManagementServersMetric UUID: %s!", id) diff --git a/cloudstack/NATService.go b/cloudstack/NATService.go index 953306bd..0d6e304d 100644 --- a/cloudstack/NATService.go +++ b/cloudstack/NATService.go @@ -916,6 +916,9 @@ func (s *NATService) GetIpForwardingRuleByID(id string, opts ...OptionFunc) (*Ip } if l.Count == 1 { + if len(l.IpForwardingRules) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.IpForwardingRules[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for IpForwardingRule UUID: %s!", id) diff --git a/cloudstack/NetscalerService.go b/cloudstack/NetscalerService.go index dc17de7d..b3b38ff5 100644 --- a/cloudstack/NetscalerService.go +++ b/cloudstack/NetscalerService.go @@ -990,6 +990,9 @@ func (s *NetscalerService) GetNetscalerLoadBalancerNetworkID(keyword string, lbd } if l.Count == 1 { + if len(l.NetscalerLoadBalancerNetworks) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.NetscalerLoadBalancerNetworks[0].Id, l.Count, nil } diff --git a/cloudstack/NetworkACLService.go b/cloudstack/NetworkACLService.go index b64e8731..73d98537 100644 --- a/cloudstack/NetworkACLService.go +++ b/cloudstack/NetworkACLService.go @@ -1139,6 +1139,9 @@ func (s *NetworkACLService) GetNetworkACLListID(name string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.NetworkACLLists) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.NetworkACLLists[0].Id, l.Count, nil } @@ -1194,6 +1197,9 @@ func (s *NetworkACLService) GetNetworkACLListByID(id string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.NetworkACLLists) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.NetworkACLLists[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for NetworkACLList UUID: %s!", id) @@ -1671,6 +1677,9 @@ func (s *NetworkACLService) GetNetworkACLByID(id string, opts ...OptionFunc) (*N } if l.Count == 1 { + if len(l.NetworkACLs) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.NetworkACLs[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for NetworkACL UUID: %s!", id) diff --git a/cloudstack/NetworkOfferingService.go b/cloudstack/NetworkOfferingService.go index 2d670bd0..8551c1a2 100644 --- a/cloudstack/NetworkOfferingService.go +++ b/cloudstack/NetworkOfferingService.go @@ -1617,6 +1617,9 @@ func (s *NetworkOfferingService) GetNetworkOfferingID(name string, opts ...Optio } if l.Count == 1 { + if len(l.NetworkOfferings) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.NetworkOfferings[0].Id, l.Count, nil } @@ -1672,6 +1675,9 @@ func (s *NetworkOfferingService) GetNetworkOfferingByID(id string, opts ...Optio } if l.Count == 1 { + if len(l.NetworkOfferings) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.NetworkOfferings[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for NetworkOffering UUID: %s!", id) diff --git a/cloudstack/NetworkService.go b/cloudstack/NetworkService.go index 96f1a2e1..9097b3bc 100644 --- a/cloudstack/NetworkService.go +++ b/cloudstack/NetworkService.go @@ -3620,6 +3620,9 @@ func (s *NetworkService) GetIpv4SubnetsForGuestNetworkByID(id string, opts ...Op } if l.Count == 1 { + if len(l.Ipv4SubnetsForGuestNetwork) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Ipv4SubnetsForGuestNetwork[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Ipv4SubnetsForGuestNetwork UUID: %s!", id) @@ -4047,6 +4050,9 @@ func (s *NetworkService) GetNetworkServiceProviderID(name string, opts ...Option } if l.Count == 1 { + if len(l.NetworkServiceProviders) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.NetworkServiceProviders[0].Id, l.Count, nil } @@ -4872,6 +4878,9 @@ func (s *NetworkService) GetNetworkID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.Networks) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Networks[0].Id, l.Count, nil } @@ -4927,6 +4936,9 @@ func (s *NetworkService) GetNetworkByID(id string, opts ...OptionFunc) (*Network } if l.Count == 1 { + if len(l.Networks) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Networks[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Network UUID: %s!", id) @@ -5199,6 +5211,9 @@ func (s *NetworkService) GetNiciraNvpDeviceNetworkID(keyword string, nvpdeviceid } if l.Count == 1 { + if len(l.NiciraNvpDeviceNetworks) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.NiciraNvpDeviceNetworks[0].Id, l.Count, nil } @@ -5432,6 +5447,9 @@ func (s *NetworkService) GetOpenDaylightControllerByID(id string, opts ...Option } if l.Count == 1 { + if len(l.OpenDaylightControllers) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.OpenDaylightControllers[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for OpenDaylightController UUID: %s!", id) @@ -5610,6 +5628,9 @@ func (s *NetworkService) GetPaloAltoFirewallNetworkID(keyword string, lbdeviceid } if l.Count == 1 { + if len(l.PaloAltoFirewallNetworks) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.PaloAltoFirewallNetworks[0].Id, l.Count, nil } @@ -5936,6 +5957,9 @@ func (s *NetworkService) GetPhysicalNetworkID(name string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.PhysicalNetworks) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.PhysicalNetworks[0].Id, l.Count, nil } @@ -5991,6 +6015,9 @@ func (s *NetworkService) GetPhysicalNetworkByID(id string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.PhysicalNetworks) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.PhysicalNetworks[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for PhysicalNetwork UUID: %s!", id) @@ -6226,6 +6253,9 @@ func (s *NetworkService) GetStorageNetworkIpRangeByID(id string, opts ...OptionF } if l.Count == 1 { + if len(l.StorageNetworkIpRange) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.StorageNetworkIpRange[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for StorageNetworkIpRange UUID: %s!", id) @@ -8453,6 +8483,9 @@ func (s *NetworkService) GetGuestNetworkIpv6PrefixeByID(id string, opts ...Optio } if l.Count == 1 { + if len(l.GuestNetworkIpv6Prefixes) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.GuestNetworkIpv6Prefixes[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for GuestNetworkIpv6Prefixe UUID: %s!", id) diff --git a/cloudstack/OauthService.go b/cloudstack/OauthService.go index ab175178..2ac25f93 100644 --- a/cloudstack/OauthService.go +++ b/cloudstack/OauthService.go @@ -204,6 +204,9 @@ func (s *OauthService) GetOauthProviderID(keyword string, opts ...OptionFunc) (s } if l.Count == 1 { + if len(l.OauthProvider) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.OauthProvider[0].Id, l.Count, nil } @@ -259,6 +262,9 @@ func (s *OauthService) GetOauthProviderByID(id string, opts ...OptionFunc) (*Oau } if l.Count == 1 { + if len(l.OauthProvider) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.OauthProvider[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for OauthProvider UUID: %s!", id) diff --git a/cloudstack/ObjectStoreService.go b/cloudstack/ObjectStoreService.go index 2422e000..27d4163a 100644 --- a/cloudstack/ObjectStoreService.go +++ b/cloudstack/ObjectStoreService.go @@ -1023,6 +1023,9 @@ func (s *ObjectStoreService) GetBucketID(name string, opts ...OptionFunc) (strin } if l.Count == 1 { + if len(l.Buckets) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Buckets[0].Id, l.Count, nil } @@ -1078,6 +1081,9 @@ func (s *ObjectStoreService) GetBucketByID(id string, opts ...OptionFunc) (*Buck } if l.Count == 1 { + if len(l.Buckets) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Buckets[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Bucket UUID: %s!", id) diff --git a/cloudstack/OvsElementService.go b/cloudstack/OvsElementService.go index 3cee21ac..7bbf535d 100644 --- a/cloudstack/OvsElementService.go +++ b/cloudstack/OvsElementService.go @@ -350,6 +350,9 @@ func (s *OvsElementService) GetOvsElementByID(id string, opts ...OptionFunc) (*O } if l.Count == 1 { + if len(l.OvsElements) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.OvsElements[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for OvsElement UUID: %s!", id) diff --git a/cloudstack/PodService.go b/cloudstack/PodService.go index 9bf7fb79..098318b7 100644 --- a/cloudstack/PodService.go +++ b/cloudstack/PodService.go @@ -1481,6 +1481,9 @@ func (s *PodService) GetPodID(name string, opts ...OptionFunc) (string, int, err } if l.Count == 1 { + if len(l.Pods) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Pods[0].Id, l.Count, nil } @@ -1536,6 +1539,9 @@ func (s *PodService) GetPodByID(id string, opts ...OptionFunc) (*Pod, int, error } if l.Count == 1 { + if len(l.Pods) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Pods[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Pod UUID: %s!", id) diff --git a/cloudstack/PoolService.go b/cloudstack/PoolService.go index 9ea2f50c..9ee325cb 100644 --- a/cloudstack/PoolService.go +++ b/cloudstack/PoolService.go @@ -1270,6 +1270,9 @@ func (s *PoolService) GetStoragePoolID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.StoragePools) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.StoragePools[0].Id, l.Count, nil } @@ -1325,6 +1328,9 @@ func (s *PoolService) GetStoragePoolByID(id string, opts ...OptionFunc) (*Storag } if l.Count == 1 { + if len(l.StoragePools) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.StoragePools[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for StoragePool UUID: %s!", id) diff --git a/cloudstack/PortableIPService.go b/cloudstack/PortableIPService.go index 92ec0e3a..7229407a 100644 --- a/cloudstack/PortableIPService.go +++ b/cloudstack/PortableIPService.go @@ -521,6 +521,9 @@ func (s *PortableIPService) GetPortableIpRangeByID(id string, opts ...OptionFunc } if l.Count == 1 { + if len(l.PortableIpRanges) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.PortableIpRanges[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for PortableIpRange UUID: %s!", id) diff --git a/cloudstack/ProjectService.go b/cloudstack/ProjectService.go index bbafee4f..c3499f38 100644 --- a/cloudstack/ProjectService.go +++ b/cloudstack/ProjectService.go @@ -1590,6 +1590,9 @@ func (s *ProjectService) GetProjectInvitationByID(id string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.ProjectInvitations) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ProjectInvitations[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ProjectInvitation UUID: %s!", id) @@ -2043,6 +2046,9 @@ func (s *ProjectService) GetProjectID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.Projects) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Projects[0].Id, l.Count, nil } @@ -2098,6 +2104,9 @@ func (s *ProjectService) GetProjectByID(id string, opts ...OptionFunc) (*Project } if l.Count == 1 { + if len(l.Projects) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Projects[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Project UUID: %s!", id) diff --git a/cloudstack/ResourcetagsService.go b/cloudstack/ResourcetagsService.go index 8f61ac3d..c8885541 100644 --- a/cloudstack/ResourcetagsService.go +++ b/cloudstack/ResourcetagsService.go @@ -455,6 +455,9 @@ func (s *ResourcetagsService) GetStorageTagID(keyword string, opts ...OptionFunc } if l.Count == 1 { + if len(l.StorageTags) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.StorageTags[0].Id, l.Count, nil } diff --git a/cloudstack/RoleService.go b/cloudstack/RoleService.go index 62e41557..8a398458 100644 --- a/cloudstack/RoleService.go +++ b/cloudstack/RoleService.go @@ -1241,6 +1241,9 @@ func (s *RoleService) GetRoleID(name string, opts ...OptionFunc) (string, int, e } if l.Count == 1 { + if len(l.Roles) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Roles[0].Id, l.Count, nil } @@ -1296,6 +1299,9 @@ func (s *RoleService) GetRoleByID(id string, opts ...OptionFunc) (*Role, int, er } if l.Count == 1 { + if len(l.Roles) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Roles[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Role UUID: %s!", id) @@ -1863,6 +1869,9 @@ func (s *RoleService) GetProjectRoleID(name string, projectid string, opts ...Op } if l.Count == 1 { + if len(l.ProjectRoles) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ProjectRoles[0].Id, l.Count, nil } diff --git a/cloudstack/RouterService.go b/cloudstack/RouterService.go index ed27a8c0..94bb8d79 100644 --- a/cloudstack/RouterService.go +++ b/cloudstack/RouterService.go @@ -1265,6 +1265,9 @@ func (s *RouterService) GetRouterID(name string, opts ...OptionFunc) (string, in } if l.Count == 1 { + if len(l.Routers) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Routers[0].Id, l.Count, nil } @@ -1320,6 +1323,9 @@ func (s *RouterService) GetRouterByID(id string, opts ...OptionFunc) (*Router, i } if l.Count == 1 { + if len(l.Routers) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Routers[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Router UUID: %s!", id) @@ -1609,6 +1615,9 @@ func (s *RouterService) GetVirtualRouterElementByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.VirtualRouterElements) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VirtualRouterElements[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VirtualRouterElement UUID: %s!", id) diff --git a/cloudstack/SSHService.go b/cloudstack/SSHService.go index 30038d47..769b71a4 100644 --- a/cloudstack/SSHService.go +++ b/cloudstack/SSHService.go @@ -671,6 +671,9 @@ func (s *SSHService) GetSSHKeyPairID(name string, opts ...OptionFunc) (string, i } if l.Count == 1 { + if len(l.SSHKeyPairs) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.SSHKeyPairs[0].Id, l.Count, nil } @@ -726,6 +729,9 @@ func (s *SSHService) GetSSHKeyPairByID(id string, opts ...OptionFunc) (*SSHKeyPa } if l.Count == 1 { + if len(l.SSHKeyPairs) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.SSHKeyPairs[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for SSHKeyPair UUID: %s!", id) diff --git a/cloudstack/SecurityGroupService.go b/cloudstack/SecurityGroupService.go index 7ac92295..b172d4fa 100644 --- a/cloudstack/SecurityGroupService.go +++ b/cloudstack/SecurityGroupService.go @@ -1545,6 +1545,9 @@ func (s *SecurityGroupService) GetSecurityGroupID(keyword string, opts ...Option } if l.Count == 1 { + if len(l.SecurityGroups) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.SecurityGroups[0].Id, l.Count, nil } @@ -1600,6 +1603,9 @@ func (s *SecurityGroupService) GetSecurityGroupByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.SecurityGroups) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.SecurityGroups[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for SecurityGroup UUID: %s!", id) diff --git a/cloudstack/ServiceOfferingService.go b/cloudstack/ServiceOfferingService.go index 372b4ed6..6c647ece 100644 --- a/cloudstack/ServiceOfferingService.go +++ b/cloudstack/ServiceOfferingService.go @@ -2194,6 +2194,9 @@ func (s *ServiceOfferingService) GetServiceOfferingID(name string, opts ...Optio } if l.Count == 1 { + if len(l.ServiceOfferings) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ServiceOfferings[0].Id, l.Count, nil } @@ -2249,6 +2252,9 @@ func (s *ServiceOfferingService) GetServiceOfferingByID(id string, opts ...Optio } if l.Count == 1 { + if len(l.ServiceOfferings) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ServiceOfferings[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ServiceOffering UUID: %s!", id) diff --git a/cloudstack/SharedFileSystemService.go b/cloudstack/SharedFileSystemService.go index 143aa7c5..8d131d5f 100644 --- a/cloudstack/SharedFileSystemService.go +++ b/cloudstack/SharedFileSystemService.go @@ -1650,6 +1650,9 @@ func (s *SharedFileSystemService) GetSharedFileSystemID(name string, opts ...Opt } if l.Count == 1 { + if len(l.SharedFileSystems) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.SharedFileSystems[0].Id, l.Count, nil } @@ -1705,6 +1708,9 @@ func (s *SharedFileSystemService) GetSharedFileSystemByID(id string, opts ...Opt } if l.Count == 1 { + if len(l.SharedFileSystems) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.SharedFileSystems[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for SharedFileSystem UUID: %s!", id) diff --git a/cloudstack/SnapshotService.go b/cloudstack/SnapshotService.go index ab11f368..046511a2 100644 --- a/cloudstack/SnapshotService.go +++ b/cloudstack/SnapshotService.go @@ -2351,6 +2351,9 @@ func (s *SnapshotService) GetSnapshotPolicyByID(id string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.SnapshotPolicies) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.SnapshotPolicies[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for SnapshotPolicy UUID: %s!", id) @@ -2926,6 +2929,9 @@ func (s *SnapshotService) GetSnapshotID(name string, opts ...OptionFunc) (string } if l.Count == 1 { + if len(l.Snapshots) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Snapshots[0].Id, l.Count, nil } @@ -2981,6 +2987,9 @@ func (s *SnapshotService) GetSnapshotByID(id string, opts ...OptionFunc) (*Snaps } if l.Count == 1 { + if len(l.Snapshots) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Snapshots[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Snapshot UUID: %s!", id) @@ -3459,6 +3468,9 @@ func (s *SnapshotService) GetVMSnapshotID(name string, opts ...OptionFunc) (stri } if l.Count == 1 { + if len(l.VMSnapshot) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VMSnapshot[0].Id, l.Count, nil } diff --git a/cloudstack/StoragePoolService.go b/cloudstack/StoragePoolService.go index 9a1c0ff2..e4646334 100644 --- a/cloudstack/StoragePoolService.go +++ b/cloudstack/StoragePoolService.go @@ -598,6 +598,9 @@ func (s *StoragePoolService) GetAffectedVmsForStorageScopeChangeID(keyword strin } if l.Count == 1 { + if len(l.AffectedVmsForStorageScopeChange) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.AffectedVmsForStorageScopeChange[0].Id, l.Count, nil } @@ -978,6 +981,9 @@ func (s *StoragePoolService) GetObjectStoragePoolID(name string, opts ...OptionF } if l.Count == 1 { + if len(l.ObjectStoragePools) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ObjectStoragePools[0].Id, l.Count, nil } @@ -1033,6 +1039,9 @@ func (s *StoragePoolService) GetObjectStoragePoolByID(id string, opts ...OptionF } if l.Count == 1 { + if len(l.ObjectStoragePools) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ObjectStoragePools[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ObjectStoragePool UUID: %s!", id) @@ -1242,6 +1251,9 @@ func (s *StoragePoolService) GetStoragePoolObjectByID(id string, opts ...OptionF } if l.Count == 1 { + if len(l.StoragePoolObjects) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.StoragePoolObjects[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for StoragePoolObject UUID: %s!", id) @@ -2135,6 +2147,9 @@ func (s *StoragePoolService) GetStoragePoolsMetricID(name string, opts ...Option } if l.Count == 1 { + if len(l.StoragePoolsMetrics) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.StoragePoolsMetrics[0].Id, l.Count, nil } @@ -2190,6 +2205,9 @@ func (s *StoragePoolService) GetStoragePoolsMetricByID(id string, opts ...Option } if l.Count == 1 { + if len(l.StoragePoolsMetrics) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.StoragePoolsMetrics[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for StoragePoolsMetric UUID: %s!", id) diff --git a/cloudstack/SwiftService.go b/cloudstack/SwiftService.go index 01f8bdd8..49baf6af 100644 --- a/cloudstack/SwiftService.go +++ b/cloudstack/SwiftService.go @@ -325,6 +325,9 @@ func (s *SwiftService) GetSwiftID(keyword string, opts ...OptionFunc) (string, i } if l.Count == 1 { + if len(l.Swifts) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.Swifts[0].Id, l.Count, nil } diff --git a/cloudstack/SystemVMService.go b/cloudstack/SystemVMService.go index f024d065..5cc51815 100644 --- a/cloudstack/SystemVMService.go +++ b/cloudstack/SystemVMService.go @@ -669,6 +669,9 @@ func (s *SystemVMService) GetSystemVmID(name string, opts ...OptionFunc) (string } if l.Count == 1 { + if len(l.SystemVms) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.SystemVms[0].Id, l.Count, nil } @@ -724,6 +727,9 @@ func (s *SystemVMService) GetSystemVmByID(id string, opts ...OptionFunc) (*Syste } if l.Count == 1 { + if len(l.SystemVms) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.SystemVms[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for SystemVm UUID: %s!", id) @@ -1031,6 +1037,9 @@ func (s *SystemVMService) GetSystemVmsUsageHistoryID(name string, opts ...Option } if l.Count == 1 { + if len(l.SystemVmsUsageHistory) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.SystemVmsUsageHistory[0].Id, l.Count, nil } @@ -1086,6 +1095,9 @@ func (s *SystemVMService) GetSystemVmsUsageHistoryByID(id string, opts ...Option } if l.Count == 1 { + if len(l.SystemVmsUsageHistory) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.SystemVmsUsageHistory[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for SystemVmsUsageHistory UUID: %s!", id) diff --git a/cloudstack/TemplateService.go b/cloudstack/TemplateService.go index e9f90712..234a59a8 100644 --- a/cloudstack/TemplateService.go +++ b/cloudstack/TemplateService.go @@ -2031,6 +2031,9 @@ func (s *TemplateService) GetTemplatePermissionByID(id string, opts ...OptionFun } if l.Count == 1 { + if len(l.TemplatePermissions) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.TemplatePermissions[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for TemplatePermission UUID: %s!", id) @@ -2824,6 +2827,9 @@ func (s *TemplateService) GetTemplateID(name string, templatefilter string, zone } if l.Count == 1 { + if len(l.Templates) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Templates[0].Id, l.Count, nil } @@ -2880,6 +2886,9 @@ func (s *TemplateService) GetTemplateByID(id string, templatefilter string, opts } if l.Count == 1 { + if len(l.Templates) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Templates[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Template UUID: %s!", id) diff --git a/cloudstack/UCSService.go b/cloudstack/UCSService.go index 0cbbd3d9..31149cc7 100644 --- a/cloudstack/UCSService.go +++ b/cloudstack/UCSService.go @@ -761,6 +761,9 @@ func (s *UCSService) GetUcsManagerID(keyword string, opts ...OptionFunc) (string } if l.Count == 1 { + if len(l.UcsManagers) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.UcsManagers[0].Id, l.Count, nil } @@ -816,6 +819,9 @@ func (s *UCSService) GetUcsManagerByID(id string, opts ...OptionFunc) (*UcsManag } if l.Count == 1 { + if len(l.UcsManagers) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.UcsManagers[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for UcsManager UUID: %s!", id) diff --git a/cloudstack/UsageService.go b/cloudstack/UsageService.go index ef89ced3..21dbec97 100644 --- a/cloudstack/UsageService.go +++ b/cloudstack/UsageService.go @@ -1239,6 +1239,9 @@ func (s *UsageService) GetTrafficTypeID(keyword string, physicalnetworkid string } if l.Count == 1 { + if len(l.TrafficTypes) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.TrafficTypes[0].Id, l.Count, nil } diff --git a/cloudstack/UserService.go b/cloudstack/UserService.go index 1b46c0b2..3fedb7a2 100644 --- a/cloudstack/UserService.go +++ b/cloudstack/UserService.go @@ -1355,6 +1355,9 @@ func (s *UserService) GetUserByID(id string, opts ...OptionFunc) (*User, int, er } if l.Count == 1 { + if len(l.Users) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Users[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for User UUID: %s!", id) @@ -2218,6 +2221,9 @@ func (s *UserService) GetUserDataID(name string, opts ...OptionFunc) (string, in } if l.Count == 1 { + if len(l.UserData) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.UserData[0].Id, l.Count, nil } @@ -2273,6 +2279,9 @@ func (s *UserService) GetUserDataByID(id string, opts ...OptionFunc) (*UserData, } if l.Count == 1 { + if len(l.UserData) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.UserData[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for UserData UUID: %s!", id) diff --git a/cloudstack/VLANService.go b/cloudstack/VLANService.go index 68b0a31a..9ddf8d49 100644 --- a/cloudstack/VLANService.go +++ b/cloudstack/VLANService.go @@ -1136,6 +1136,9 @@ func (s *VLANService) GetDedicatedGuestVlanRangeByID(id string, opts ...OptionFu } if l.Count == 1 { + if len(l.DedicatedGuestVlanRanges) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.DedicatedGuestVlanRanges[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for DedicatedGuestVlanRange UUID: %s!", id) @@ -1539,6 +1542,9 @@ func (s *VLANService) GetVlanIpRangeByID(id string, opts ...OptionFunc) (*VlanIp } if l.Count == 1 { + if len(l.VlanIpRanges) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VlanIpRanges[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VlanIpRange UUID: %s!", id) diff --git a/cloudstack/VMGroupService.go b/cloudstack/VMGroupService.go index c64a1794..50c5bbdf 100644 --- a/cloudstack/VMGroupService.go +++ b/cloudstack/VMGroupService.go @@ -569,6 +569,9 @@ func (s *VMGroupService) GetInstanceGroupID(name string, opts ...OptionFunc) (st } if l.Count == 1 { + if len(l.InstanceGroups) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.InstanceGroups[0].Id, l.Count, nil } @@ -624,6 +627,9 @@ func (s *VMGroupService) GetInstanceGroupByID(id string, opts ...OptionFunc) (*I } if l.Count == 1 { + if len(l.InstanceGroups) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.InstanceGroups[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for InstanceGroup UUID: %s!", id) diff --git a/cloudstack/VPCService.go b/cloudstack/VPCService.go index d5f67d05..561af8e3 100644 --- a/cloudstack/VPCService.go +++ b/cloudstack/VPCService.go @@ -2423,6 +2423,9 @@ func (s *VPCService) GetPrivateGatewayByID(id string, opts ...OptionFunc) (*Priv } if l.Count == 1 { + if len(l.PrivateGateways) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.PrivateGateways[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for PrivateGateway UUID: %s!", id) @@ -2844,6 +2847,9 @@ func (s *VPCService) GetStaticRouteByID(id string, opts ...OptionFunc) (*StaticR } if l.Count == 1 { + if len(l.StaticRoutes) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.StaticRoutes[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for StaticRoute UUID: %s!", id) @@ -3199,6 +3205,9 @@ func (s *VPCService) GetVPCOfferingID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.VPCOfferings) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VPCOfferings[0].Id, l.Count, nil } @@ -3254,6 +3263,9 @@ func (s *VPCService) GetVPCOfferingByID(id string, opts ...OptionFunc) (*VPCOffe } if l.Count == 1 { + if len(l.VPCOfferings) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VPCOfferings[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VPCOffering UUID: %s!", id) @@ -3859,6 +3871,9 @@ func (s *VPCService) GetVPCID(name string, opts ...OptionFunc) (string, int, err } if l.Count == 1 { + if len(l.VPCs) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VPCs[0].Id, l.Count, nil } @@ -3914,6 +3929,9 @@ func (s *VPCService) GetVPCByID(id string, opts ...OptionFunc) (*VPC, int, error } if l.Count == 1 { + if len(l.VPCs) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VPCs[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VPC UUID: %s!", id) diff --git a/cloudstack/VPNService.go b/cloudstack/VPNService.go index 785c1cd9..c823bc33 100644 --- a/cloudstack/VPNService.go +++ b/cloudstack/VPNService.go @@ -1941,6 +1941,9 @@ func (s *VPNService) GetRemoteAccessVpnByID(id string, opts ...OptionFunc) (*Rem } if l.Count == 1 { + if len(l.RemoteAccessVpns) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.RemoteAccessVpns[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for RemoteAccessVpn UUID: %s!", id) @@ -2301,6 +2304,9 @@ func (s *VPNService) GetVpnConnectionByID(id string, opts ...OptionFunc) (*VpnCo } if l.Count == 1 { + if len(l.VpnConnections) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VpnConnections[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VpnConnection UUID: %s!", id) @@ -2620,6 +2626,9 @@ func (s *VPNService) GetVpnCustomerGatewayID(keyword string, opts ...OptionFunc) } if l.Count == 1 { + if len(l.VpnCustomerGateways) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.VpnCustomerGateways[0].Id, l.Count, nil } @@ -2675,6 +2684,9 @@ func (s *VPNService) GetVpnCustomerGatewayByID(id string, opts ...OptionFunc) (* } if l.Count == 1 { + if len(l.VpnCustomerGateways) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VpnCustomerGateways[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VpnCustomerGateway UUID: %s!", id) @@ -3044,6 +3056,9 @@ func (s *VPNService) GetVpnGatewayByID(id string, opts ...OptionFunc) (*VpnGatew } if l.Count == 1 { + if len(l.VpnGateways) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VpnGateways[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VpnGateway UUID: %s!", id) @@ -3378,6 +3393,9 @@ func (s *VPNService) GetVpnUserByID(id string, opts ...OptionFunc) (*VpnUser, in } if l.Count == 1 { + if len(l.VpnUsers) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VpnUsers[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VpnUser UUID: %s!", id) diff --git a/cloudstack/VirtualMachineService.go b/cloudstack/VirtualMachineService.go index 675b5c89..3068bfb1 100644 --- a/cloudstack/VirtualMachineService.go +++ b/cloudstack/VirtualMachineService.go @@ -4616,6 +4616,9 @@ func (s *VirtualMachineService) GetVirtualMachineID(name string, opts ...OptionF } if l.Count == 1 { + if len(l.VirtualMachines) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VirtualMachines[0].Id, l.Count, nil } @@ -4671,6 +4674,9 @@ func (s *VirtualMachineService) GetVirtualMachineByID(id string, opts ...OptionF } if l.Count == 1 { + if len(l.VirtualMachines) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VirtualMachines[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VirtualMachine UUID: %s!", id) @@ -6020,6 +6026,9 @@ func (s *VirtualMachineService) GetVirtualMachinesMetricID(name string, opts ... } if l.Count == 1 { + if len(l.VirtualMachinesMetrics) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VirtualMachinesMetrics[0].Id, l.Count, nil } @@ -6075,6 +6084,9 @@ func (s *VirtualMachineService) GetVirtualMachinesMetricByID(id string, opts ... } if l.Count == 1 { + if len(l.VirtualMachinesMetrics) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VirtualMachinesMetrics[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VirtualMachinesMetric UUID: %s!", id) @@ -11373,6 +11385,9 @@ func (s *VirtualMachineService) GetVirtualMachinesUsageHistoryID(name string, op } if l.Count == 1 { + if len(l.VirtualMachinesUsageHistory) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VirtualMachinesUsageHistory[0].Id, l.Count, nil } @@ -11428,6 +11443,9 @@ func (s *VirtualMachineService) GetVirtualMachinesUsageHistoryByID(id string, op } if l.Count == 1 { + if len(l.VirtualMachinesUsageHistory) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VirtualMachinesUsageHistory[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VirtualMachinesUsageHistory UUID: %s!", id) @@ -14562,6 +14580,9 @@ func (s *VirtualMachineService) GetVMScheduleByID(id string, virtualmachineid st } if l.Count == 1 { + if len(l.VMSchedule) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VMSchedule[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VMSchedule UUID: %s!", id) diff --git a/cloudstack/VirtualNetworkFunctionsService.go b/cloudstack/VirtualNetworkFunctionsService.go index 90f7deee..a124b6a5 100644 --- a/cloudstack/VirtualNetworkFunctionsService.go +++ b/cloudstack/VirtualNetworkFunctionsService.go @@ -2934,6 +2934,9 @@ func (s *VirtualNetworkFunctionsService) GetVnfApplianceID(name string, opts ... } if l.Count == 1 { + if len(l.VnfAppliances) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VnfAppliances[0].Id, l.Count, nil } @@ -2989,6 +2992,9 @@ func (s *VirtualNetworkFunctionsService) GetVnfApplianceByID(id string, opts ... } if l.Count == 1 { + if len(l.VnfAppliances) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VnfAppliances[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VnfAppliance UUID: %s!", id) @@ -3904,6 +3910,9 @@ func (s *VirtualNetworkFunctionsService) GetVnfTemplateID(name string, templatef } if l.Count == 1 { + if len(l.VnfTemplates) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VnfTemplates[0].Id, l.Count, nil } @@ -3960,6 +3969,9 @@ func (s *VirtualNetworkFunctionsService) GetVnfTemplateByID(id string, templatef } if l.Count == 1 { + if len(l.VnfTemplates) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VnfTemplates[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VnfTemplate UUID: %s!", id) diff --git a/cloudstack/VolumeService.go b/cloudstack/VolumeService.go index c9158aa5..5e4e4b10 100644 --- a/cloudstack/VolumeService.go +++ b/cloudstack/VolumeService.go @@ -2657,6 +2657,9 @@ func (s *VolumeService) GetElastistorVolumeByID(id string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.ElastistorVolume) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ElastistorVolume[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ElastistorVolume UUID: %s!", id) @@ -3373,6 +3376,9 @@ func (s *VolumeService) GetVolumeID(name string, opts ...OptionFunc) (string, in } if l.Count == 1 { + if len(l.Volumes) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Volumes[0].Id, l.Count, nil } @@ -3428,6 +3434,9 @@ func (s *VolumeService) GetVolumeByID(id string, opts ...OptionFunc) (*Volume, i } if l.Count == 1 { + if len(l.Volumes) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Volumes[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Volume UUID: %s!", id) @@ -4389,6 +4398,9 @@ func (s *VolumeService) GetVolumesMetricID(name string, opts ...OptionFunc) (str } if l.Count == 1 { + if len(l.VolumesMetrics) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VolumesMetrics[0].Id, l.Count, nil } @@ -4444,6 +4456,9 @@ func (s *VolumeService) GetVolumesMetricByID(id string, opts ...OptionFunc) (*Vo } if l.Count == 1 { + if len(l.VolumesMetrics) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VolumesMetrics[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VolumesMetric UUID: %s!", id) @@ -6262,6 +6277,9 @@ func (s *VolumeService) GetVolumesUsageHistoryID(name string, opts ...OptionFunc } if l.Count == 1 { + if len(l.VolumesUsageHistory) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.VolumesUsageHistory[0].Id, l.Count, nil } @@ -6317,6 +6335,9 @@ func (s *VolumeService) GetVolumesUsageHistoryByID(id string, opts ...OptionFunc } if l.Count == 1 { + if len(l.VolumesUsageHistory) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.VolumesUsageHistory[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for VolumesUsageHistory UUID: %s!", id) diff --git a/cloudstack/WebhookService.go b/cloudstack/WebhookService.go index 3b295cd1..5de0822f 100644 --- a/cloudstack/WebhookService.go +++ b/cloudstack/WebhookService.go @@ -1117,6 +1117,9 @@ func (s *WebhookService) GetWebhookDeliveryID(keyword string, opts ...OptionFunc } if l.Count == 1 { + if len(l.WebhookDeliveries) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.WebhookDeliveries[0].Id, l.Count, nil } @@ -1172,6 +1175,9 @@ func (s *WebhookService) GetWebhookDeliveryByID(id string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.WebhookDeliveries) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.WebhookDeliveries[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for WebhookDelivery UUID: %s!", id) @@ -1552,6 +1558,9 @@ func (s *WebhookService) GetWebhookID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.Webhooks) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Webhooks[0].Id, l.Count, nil } @@ -1607,6 +1616,9 @@ func (s *WebhookService) GetWebhookByID(id string, opts ...OptionFunc) (*Webhook } if l.Count == 1 { + if len(l.Webhooks) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Webhooks[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Webhook UUID: %s!", id) diff --git a/cloudstack/ZoneService.go b/cloudstack/ZoneService.go index 8ae1e103..a5fc1266 100644 --- a/cloudstack/ZoneService.go +++ b/cloudstack/ZoneService.go @@ -2091,6 +2091,9 @@ func (s *ZoneService) GetIpv4SubnetsForZoneByID(id string, opts ...OptionFunc) ( } if l.Count == 1 { + if len(l.Ipv4SubnetsForZone) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Ipv4SubnetsForZone[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Ipv4SubnetsForZone UUID: %s!", id) @@ -2496,6 +2499,9 @@ func (s *ZoneService) GetZoneID(name string, opts ...OptionFunc) (string, int, e } if l.Count == 1 { + if len(l.Zones) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.Zones[0].Id, l.Count, nil } @@ -2551,6 +2557,9 @@ func (s *ZoneService) GetZoneByID(id string, opts ...OptionFunc) (*Zone, int, er } if l.Count == 1 { + if len(l.Zones) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.Zones[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for Zone UUID: %s!", id) @@ -2999,6 +3008,9 @@ func (s *ZoneService) GetZonesMetricID(name string, opts ...OptionFunc) (string, } if l.Count == 1 { + if len(l.ZonesMetrics) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l) + } return l.ZonesMetrics[0].Id, l.Count, nil } @@ -3054,6 +3066,9 @@ func (s *ZoneService) GetZonesMetricByID(id string, opts ...OptionFunc) (*ZonesM } if l.Count == 1 { + if len(l.ZonesMetrics) == 0 { + return nil, l.Count, fmt.Errorf("No match found for %s: %+v", id, l) + } return l.ZonesMetrics[0], l.Count, nil } return nil, l.Count, fmt.Errorf("There is more then one result for ZonesMetric UUID: %s!", id) @@ -4476,6 +4491,9 @@ func (s *ZoneService) GetVmwareDcID(keyword string, zoneid string, opts ...Optio } if l.Count == 1 { + if len(l.VmwareDcs) == 0 { + return "", l.Count, fmt.Errorf("No match found for %s: %+v", keyword, l) + } return l.VmwareDcs[0].Id, l.Count, nil } diff --git a/generate/generate.go b/generate/generate.go index 448b6144..5959396b 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -1609,6 +1609,9 @@ func (s *service) generateHelperFuncs(a *API) { pn(" }") pn("") pn(" if l.Count == 1 {") + pn(" if len(l.%s) == 0 {", ln) + pn(" return \"\", l.Count, fmt.Errorf(\"No match found for %%s: %%+v\", %s, l)", v) + pn(" }") pn(" return l.%s[0].Id, l.Count, nil", ln) pn(" }") pn("") @@ -1735,6 +1738,9 @@ func (s *service) generateHelperFuncs(a *API) { pn(" }") pn("") pn(" if l.Count == 1 {") + pn(" if len(l.%s) == 0 {", ln) + pn(" return nil, l.Count, fmt.Errorf(\"No match found for %%s: %%+v\", id, l)") + pn(" }") pn(" return l.%s[0], l.Count, nil", ln) pn(" }") pn(" return nil, l.Count, fmt.Errorf(\"There is more then one result for %s UUID: %%s!\", id)", parseSingular(ln)) diff --git a/test/GetMetricIDRegression_test.go b/test/GetMetricIDRegression_test.go new file mode 100644 index 00000000..e976c081 --- /dev/null +++ b/test/GetMetricIDRegression_test.go @@ -0,0 +1,58 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package test + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/apache/cloudstack-go/v2/cloudstack" +) + +// Regression test for issue #103: the generated GetID helpers must not +// panic with "index out of range [0] with length 0" when the API reports +// count >= 1 but returns an empty result slice (a count/slice mismatch that +// occurs e.g. on metrics APIs). They should return a descriptive error instead. +func TestGetVirtualMachinesMetricIDEmptyListWithNonZeroCount(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + // count = 1, but the "virtualmachine" list is empty + fmt.Fprint(w, `{"listvirtualmachinesmetricsresponse":{"count":1,"virtualmachine":[]}}`) + })) + defer server.Close() + + client := cloudstack.NewClient(server.URL, "APIKEY", "SECRETKEY", true) + + defer func() { + if r := recover(); r != nil { + t.Fatalf("GetVirtualMachinesMetricID panicked instead of returning an error: %v", r) + } + }() + + id, count, err := client.VirtualMachine.GetVirtualMachinesMetricID("anyvm") + if err == nil { + t.Fatalf("expected an error when count=1 but the result list is empty; got id=%q count=%d", id, count) + } + if id != "" { + t.Fatalf("expected an empty id on the empty-list path, got %q", id) + } +}