// Takes a partial/full disk id or diskname func (os *OpenStack) getVolume(diskName string) (volumes.Volume, error) { sClient, err := openstack.NewBlockStorageV1(os.provider, gophercloud.EndpointOpts{ Region: os.region, }) var volume volumes.Volume if err != nil || sClient == nil { glog.Errorf("Unable to initialize cinder client for region: %s", os.region) return volume, err } err = volumes.List(sClient, nil).EachPage(func(page pagination.Page) (bool, error) { vols, err := volumes.ExtractVolumes(page) if err != nil { glog.Errorf("Failed to extract volumes: %v", err) return false, err } else { for _, v := range vols { glog.V(4).Infof("%s %s %v", v.ID, v.Name, v.Attachments) if v.Name == diskName || strings.Contains(v.ID, diskName) { volume = v return true, nil } } } // if it reached here then no disk with the given name was found. errmsg := fmt.Sprintf("Unable to find disk: %s in region %s", diskName, os.region) return false, errors.New(errmsg) }) if err != nil { glog.Errorf("Error occured getting volume: %s", diskName) return volume, err } return volume, err }
// Create a volume of given size (in GiB) func (os *OpenStack) CreateVolume(name string, size int, tags *map[string]string) (volumeName string, err error) { sClient, err := openstack.NewBlockStorageV1(os.provider, gophercloud.EndpointOpts{ Region: os.region, }) if err != nil || sClient == nil { glog.Errorf("Unable to initialize cinder client for region: %s", os.region) return "", err } opts := volumes.CreateOpts{ Name: name, Size: size, } if tags != nil { opts.Metadata = *tags } vol, err := volumes.Create(sClient, opts).Extract() if err != nil { glog.Errorf("Failed to create a %d GB volume: %v", size, err) return "", err } glog.Infof("Created volume %v", vol.ID) return vol.ID, err }
// GetSnapshots collects snapshot data by sending REST call to cinderhost:8776/v1/tenant_id/snapshots func (s ServiceV1) GetSnapshots(provider *gophercloud.ProviderClient) (map[string]types.Snapshots, error) { snaps := map[string]types.Snapshots{} client, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{}) if err != nil { return snaps, err } opts := snapshots.ListOpts{} pager := snapshots.List(client, opts) page, err := pager.AllPages() if err != nil { return snaps, err } snapshotList, err := snapshots.ExtractSnapshots(page) if err != nil { return snaps, err } for _, snapshot := range snapshotList { snapCounts := snaps["tenant_id"] snapCounts.Count += 1 snapCounts.Bytes += snapshot.Size * 1024 * 1024 * 1024 } return snaps, nil }
// GetVolumes collects volumes data by sending REST call to cinderhost:8776/v1/tenant_id/volumes func (s ServiceV1) GetVolumes(provider *gophercloud.ProviderClient) (map[string]types.Volumes, error) { vols := map[string]types.Volumes{} client, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{}) if err != nil { return vols, err } //opts := volumes.ListOpts{AllTenants: true} opts := volumes.ListOpts{} pager := volumes.List(client, opts) page, err := pager.AllPages() if err != nil { return vols, err } volumeList, err := volumes.ExtractVolumes(page) if err != nil { return vols, err } for _, volume := range volumeList { volCounts := vols["volume.OsVolTenantAttrTenantID"] volCounts.Count += 1 volCounts.Bytes += volume.Size * 1024 * 1024 * 1024 vols["volume.OsVolTenantAttrTenantID"] = volCounts } return vols, nil }
func (d *driver) Init(context types.Context, config gofig.Config) error { d.config = config fields := eff(map[string]interface{}{}) var err error if d.instanceID, err = d.getInstanceID(); err != nil { return err } fields["moduleName"] = context fields["instanceId"] = d.instanceID if d.region, err = d.getInstanceRegion(); err != nil { return err } fields["region"] = d.region d.region = strings.ToUpper(d.region) authOpts := d.getAuthOptions() fields["identityEndpoint"] = d.authURL() fields["userId"] = d.userID() fields["userName"] = d.userName() if d.password() == "" { fields["password"] = "" } else { fields["password"] = "******" } fields["tenantId"] = d.tenantID() fields["tenantName"] = d.tenantName() fields["domainId"] = d.domainID() fields["domainName"] = d.domainName() if d.provider, err = openstack.AuthenticatedClient(authOpts); err != nil { return goof.WithFieldsE(fields, "error getting authenticated client", err) } if d.client, err = openstack.NewComputeV2(d.provider, gophercloud.EndpointOpts{Region: d.region}); err != nil { goof.WithFieldsE(fields, "error getting newComputeV2", err) } if d.clientBlockStorage, err = openstack.NewBlockStorageV1(d.provider, gophercloud.EndpointOpts{Region: d.region}); err != nil { return goof.WithFieldsE(fields, "error getting newBlockStorageV1", err) } log.WithFields(fields).Info("storage driver initialized") return nil }
func newBlockClient(t *testing.T) (*gophercloud.ServiceClient, error) { ao, err := openstack.AuthOptionsFromEnv() th.AssertNoErr(t, err) client, err := openstack.AuthenticatedClient(ao) th.AssertNoErr(t, err) return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{ Region: os.Getenv("OS_REGION_NAME"), }) }
func (d *driver) Init(r *core.RexRay) error { d.r = r fields := ef() var err error if d.instanceID, err = getInstanceID(d.r.Config); err != nil { return err } fields["instanceId"] = d.instanceID if d.region, err = getInstanceRegion(d.r.Config); err != nil { return err } fields["region"] = d.region d.region = strings.ToUpper(d.region) authOpts := getAuthOptions(d.r.Config) fields["identityEndpoint"] = d.r.Config.RackspaceAuthURL fields["userId"] = d.r.Config.RackspaceUserID fields["userName"] = d.r.Config.RackspaceUserName if d.r.Config.RackspacePassword == "" { fields["password"] = "" } else { fields["password"] = "******" } fields["tenantId"] = d.r.Config.RackspaceTenantID fields["tenantName"] = d.r.Config.RackspaceTenantName fields["domainId"] = d.r.Config.RackspaceDomainID fields["domainName"] = d.r.Config.RackspaceDomainName if d.provider, err = openstack.AuthenticatedClient(authOpts); err != nil { return errors.WithFieldsE(fields, "error getting authenticated client", err) } if d.client, err = openstack.NewComputeV2(d.provider, gophercloud.EndpointOpts{Region: d.region}); err != nil { errors.WithFieldsE(fields, "error getting newComputeV2", err) } if d.clientBlockStorage, err = openstack.NewBlockStorageV1(d.provider, gophercloud.EndpointOpts{Region: d.region}); err != nil { return errors.WithFieldsE(fields, "error getting newBlockStorageV1", err) } log.WithField("provider", providerName).Info("storage driver initialized") return nil }
func (os *OpenStack) DeleteVolume(volumeName string) error { sClient, err := openstack.NewBlockStorageV1(os.provider, gophercloud.EndpointOpts{ Region: os.region, }) if err != nil || sClient == nil { glog.Errorf("Unable to initialize cinder client for region: %s", os.region) return err } err = volumes.Delete(sClient, volumeName).ExtractErr() if err != nil { glog.Errorf("Cannot delete volume %s: %v", volumeName, err) } return err }
func Init() (storagedriver.Driver, error) { instanceID, err := getInstanceID() if err != nil { return nil, fmt.Errorf("Error: %v", err) } region, err := getInstanceRegion() if err != nil { return nil, fmt.Errorf("Error: %v", err) } opts, err := openstack.AuthOptionsFromEnv() if err != nil { return nil, fmt.Errorf("Error: %v", err) } provider, err := openstack.AuthenticatedClient(opts) if err != nil { return nil, fmt.Errorf("Error: %v", err) } region = strings.ToUpper(region) client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { return nil, fmt.Errorf("Error: %v", err) } clientBlockStorage, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { return nil, fmt.Errorf("Error: %v", err) } driver := &Driver{ Provider: provider, Client: client, ClientBlockStorage: clientBlockStorage, Region: region, InstanceID: instanceID, } return driver, nil }
// GetLimits collects tenant limits by sending REST call to cinderhost:8776/v1/tenant_id/limits func (s ServiceV1) GetLimits(provider *gophercloud.ProviderClient) (types.Limits, error) { limits := types.Limits{} client, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{}) if err != nil { return limits, err } tenantLimits, err := limitsintel.Get(client, "limits").Extract() if err != nil { return limits, err } limits.MaxTotalVolumes = tenantLimits.MaxTotalVolumes limits.MaxTotalVolumeGigabytes = tenantLimits.MaxTotalVolumeGigabytes return limits, nil }
func (os *OpenStack) DeleteVolume(volumeName string) error { used, err := os.diskIsUsed(volumeName) if err != nil { return err } if used { msg := fmt.Sprintf("Cannot delete the volume %q, it's still attached to a node", volumeName) return volume.NewDeletedVolumeInUseError(msg) } sClient, err := openstack.NewBlockStorageV1(os.provider, gophercloud.EndpointOpts{ Region: os.region, }) if err != nil || sClient == nil { glog.Errorf("Unable to initialize cinder client for region: %s", os.region) return err } err = volumes.Delete(sClient, volumeName).ExtractErr() if err != nil { glog.Errorf("Cannot delete volume %s: %v", volumeName, err) } return err }
func (d *driver) Init(r *core.RexRay) error { d.r = r fields := ef() var err error if d.instanceID, err = getInstanceID(d.r.Config); err != nil { return err } fields["instanceId"] = d.instanceID if d.regionName() == "" { if d.region, err = getInstanceRegion(d.r.Config); err != nil { return err } } else { d.region = d.regionName() } fields["region"] = d.region if d.availabilityZoneName() == "" { if d.availabilityZone, err = getInstanceAvailabilityZone(); err != nil { return err } } else { d.availabilityZone = d.availabilityZoneName() } fields["availabilityZone"] = d.availabilityZone authOpts := d.getAuthOptions() fields["identityEndpoint"] = d.authURL() fields["userId"] = d.userID() fields["userName"] = d.userName() if d.password() == "" { fields["password"] = "" } else { fields["password"] = "******" } fields["tenantId"] = d.tenantID() fields["tenantName"] = d.tenantName() fields["domainId"] = d.domainID() fields["domainName"] = d.domainName() if d.provider, err = openstack.AuthenticatedClient(authOpts); err != nil { return errors.WithFieldsE(fields, "error getting authenticated client", err) } if d.client, err = openstack.NewComputeV2(d.provider, gophercloud.EndpointOpts{Region: d.region}); err != nil { errors.WithFieldsE(fields, "error getting newComputeV2", err) } if d.clientBlockStorage, err = openstack.NewBlockStorageV1(d.provider, gophercloud.EndpointOpts{Region: d.region}); err != nil { return errors.WithFieldsE(fields, "error getting newBlockStorageV1", err) } log.WithField("provider", providerName).Info("storage driver initialized") return nil }
func (c *Config) blockStorageV1Client(region string) (*gophercloud.ServiceClient, error) { return openstack.NewBlockStorageV1(c.osClient, gophercloud.EndpointOpts{ Region: region, Availability: c.getEndpointType(), }) }
func Init(cfg *config.Config) (storage.Driver, error) { fields := ef() instanceID, err := getInstanceID(cfg) if err != nil { return nil, err } fields["instanceId"] = instanceID region, err := getInstanceRegion(cfg) if err != nil { return nil, err } fields["region"] = region authOpts := getAuthOptions(cfg) fields["identityEndpoint"] = cfg.RackspaceAuthUrl fields["userId"] = cfg.RackspaceUserId fields["userName"] = cfg.RackspaceUserName if cfg.RackspacePassword == "" { fields["password"] = "" } else { fields["password"] = "******" } fields["tenantId"] = cfg.RackspaceTenantId fields["tenantName"] = cfg.RackspaceTenantName fields["domainId"] = cfg.RackspaceDomainId fields["domainName"] = cfg.RackspaceDomainName provider, err := openstack.AuthenticatedClient(authOpts) if err != nil { return nil, errors.WithFieldsE(fields, "error getting authenticated client", err) } region = strings.ToUpper(region) client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { return nil, errors.WithFieldsE(fields, "error getting newComputeV2", err) } clientBlockStorage, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { return nil, errors.WithFieldsE( fields, "error getting newBlockStorageV1", err) } driver := &Driver{ Provider: provider, Client: client, ClientBlockStorage: clientBlockStorage, Region: region, InstanceID: instanceID, Config: cfg, } return driver, nil }