mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-08-15 19:56:06 +00:00
add zone/location validation to cloudprovider constructors
Signed-off-by: Steve Kriss <steve@heptio.com>
This commit is contained in:
@@ -17,6 +17,8 @@ limitations under the License.
|
||||
package aws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
@@ -42,9 +44,22 @@ func NewStorageAdapter(config *aws.Config, availabilityZone string) (cloudprovid
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate the availabilityZone
|
||||
var (
|
||||
ec2Client = ec2.New(sess)
|
||||
azReq = &ec2.DescribeAvailabilityZonesInput{ZoneNames: []*string{&availabilityZone}}
|
||||
)
|
||||
res, err := ec2Client.DescribeAvailabilityZones(azReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(res.AvailabilityZones) == 0 {
|
||||
return nil, fmt.Errorf("availability zone %q not found", availabilityZone)
|
||||
}
|
||||
|
||||
return &storageAdapter{
|
||||
blockStorage: &blockStorageAdapter{
|
||||
ec2: ec2.New(sess),
|
||||
ec2: ec2Client,
|
||||
az: availabilityZone,
|
||||
},
|
||||
objectStorage: &objectStorageAdapter{
|
||||
|
||||
@@ -17,12 +17,14 @@ limitations under the License.
|
||||
package azure
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/arm/disk"
|
||||
"github.com/Azure/azure-sdk-for-go/arm/examples/helpers"
|
||||
"github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions"
|
||||
"github.com/Azure/azure-sdk-for-go/storage"
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
|
||||
@@ -79,6 +81,31 @@ func NewStorageAdapter(location string, apiTimeout time.Duration) (cloudprovider
|
||||
apiTimeout = time.Minute
|
||||
}
|
||||
|
||||
// validate the location
|
||||
groupClient := subscriptions.NewGroupClient()
|
||||
groupClient.Authorizer = spt
|
||||
|
||||
locs, err := groupClient.ListLocations(cfg[azureSubscriptionIDKey])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if locs.Value == nil {
|
||||
return nil, errors.New("no locations returned from Azure API")
|
||||
}
|
||||
|
||||
locationExists := false
|
||||
for _, loc := range *locs.Value {
|
||||
if (loc.Name != nil && *loc.Name == location) || (loc.DisplayName != nil && *loc.DisplayName == location) {
|
||||
locationExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !locationExists {
|
||||
return nil, fmt.Errorf("location %q not found", location)
|
||||
}
|
||||
|
||||
return &storageAdapter{
|
||||
objectStorage: &objectStorageAdapter{
|
||||
blobClient: &blobClient,
|
||||
|
||||
@@ -17,6 +17,8 @@ limitations under the License.
|
||||
package gcp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
"google.golang.org/api/compute/v0.beta"
|
||||
@@ -44,6 +46,16 @@ func NewStorageAdapter(project string, zone string) (cloudprovider.StorageAdapte
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate project & zone
|
||||
res, err := gce.Zones.Get(project, zone).Do()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res == nil {
|
||||
return nil, fmt.Errorf("zone %q not found for project %q", project, zone)
|
||||
}
|
||||
|
||||
gcs, err := storage.New(client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user