Skip to content

Commit

Permalink
Merge branch 'master' into dcos_docker_vol_secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
lpabon authored Mar 16, 2019
2 parents 20392bc + c0b02c8 commit ecea611
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 47 deletions.
136 changes: 100 additions & 36 deletions api/server/sdk/cloud_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,32 @@ func (s *CloudBackupServer) Create(
if s.driver(ctx) == nil {
return nil, status.Error(codes.Unavailable, "Resource has not been initialized")
}

credId := req.GetCredentialId()
var err error
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must supply a volume id")
} else if len(req.GetCredentialId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must supply credential uuid")
}
if len(req.GetCredentialId()) == 0 {
credId, err = s.defaultCloudBackupCreds(ctx)
if err != nil {
return nil, err
}
}

// Check ownership
if err := checkAccessFromDriverForVolumeId(ctx, s.driver(ctx), req.GetVolumeId(), api.Ownership_Read); err != nil {
return nil, err
}
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err

if len(req.GetCredentialId()) != 0 {
if err := s.checkAccessToCredential(ctx, credId); err != nil {
return nil, err
}
}

r, err := s.driver(ctx).CloudBackupCreate(&api.CloudBackupCreateRequest{
VolumeID: req.GetVolumeId(),
CredentialUUID: req.GetCredentialId(),
CredentialUUID: credId,
Full: req.GetFull(),
Name: req.GetTaskId(),
Labels: req.GetLabels(),
Expand All @@ -82,20 +90,27 @@ func (s *CloudBackupServer) Restore(
if s.driver(ctx) == nil {
return nil, status.Error(codes.Unavailable, "Resource has not been initialized")
}

credId := req.GetCredentialId()
var err error
if len(req.GetBackupId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide backup id")
} else if len(req.GetCredentialId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide credential uuid")
credId, err = s.defaultCloudBackupCreds(ctx)
if err != nil {
return nil, err
}
}
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err

if len(req.GetCredentialId()) != 0 {
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
}
}

r, err := s.driver(ctx).CloudBackupRestore(&api.CloudBackupRestoreRequest{
ID: req.GetBackupId(),
RestoreVolumeName: req.GetRestoreVolumeName(),
CredentialUUID: req.GetCredentialId(),
CredentialUUID: credId,
NodeID: req.GetNodeId(),
Name: req.GetTaskId(),
})
Expand All @@ -118,19 +133,25 @@ func (s *CloudBackupServer) Delete(
if s.driver(ctx) == nil {
return nil, status.Error(codes.Unavailable, "Resource has not been initialized")
}

credId := req.GetCredentialId()
var err error
if len(req.GetBackupId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide backup id")
} else if len(req.GetCredentialId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide credential uuid")
credId, err = s.defaultCloudBackupCreds(ctx)
if err != nil {
return nil, err
}
}
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
if len(req.GetCredentialId()) != 0 {
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
}
}

if err := s.driver(ctx).CloudBackupDelete(&api.CloudBackupDeleteRequest{
ID: req.GetBackupId(),
CredentialUUID: req.GetCredentialId(),
CredentialUUID: credId,
Force: req.GetForce(),
}); err != nil {
return nil, status.Errorf(codes.Internal, "Failed to delete backup: %v", err)
Expand All @@ -147,20 +168,25 @@ func (s *CloudBackupServer) DeleteAll(
if s.driver(ctx) == nil {
return nil, status.Error(codes.Unavailable, "Resource has not been initialized")
}

credId := req.GetCredentialId()
var err error
if len(req.GetSrcVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide source volume id")
} else if len(req.GetCredentialId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide credential uuid")
credId, err = s.defaultCloudBackupCreds(ctx)
if err != nil {
return nil, err
}
}
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
if len(req.GetCredentialId()) != 0 {
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
}
}

if err := s.driver(ctx).CloudBackupDeleteAll(&api.CloudBackupDeleteAllRequest{
CloudBackupGenericRequest: api.CloudBackupGenericRequest{
SrcVolumeID: req.GetSrcVolumeId(),
CredentialUUID: req.GetCredentialId(),
CredentialUUID: credId,
},
}); err != nil {
return nil, status.Errorf(codes.Internal, "Failed to delete backup: %v", err)
Expand All @@ -177,19 +203,25 @@ func (s *CloudBackupServer) EnumerateWithFilters(
if s.driver(ctx) == nil {
return nil, status.Error(codes.Unavailable, "Resource has not been initialized")
}

credId := req.GetCredentialId()
var err error
if len(req.GetCredentialId()) == 0 {
credId, err = s.defaultCloudBackupCreds(ctx)
if err != nil {
return nil, err
}
return nil, status.Error(codes.InvalidArgument, "Must provide credential uuid")
}
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
} else {
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
}
}

r, err := s.driver(ctx).CloudBackupEnumerate(&api.CloudBackupEnumerateRequest{
CloudBackupGenericRequest: api.CloudBackupGenericRequest{
SrcVolumeID: req.GetSrcVolumeId(),
ClusterID: req.GetClusterId(),
CredentialUUID: req.GetCredentialId(),
CredentialUUID: credId,
All: req.GetAll(),
},
})
Expand Down Expand Up @@ -236,16 +268,23 @@ func (s *CloudBackupServer) Catalog(

if len(req.GetBackupId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide backup id")
} else if len(req.GetCredentialId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must provide credential uuid")
}
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
credId := req.GetCredentialId()
var err error
if len(req.GetCredentialId()) == 0 {
credId, err = s.defaultCloudBackupCreds(ctx)
if err != nil {
return nil, err
}
} else {
if err := s.checkAccessToCredential(ctx, req.GetCredentialId()); err != nil {
return nil, err
}
}

r, err := s.driver(ctx).CloudBackupCatalog(&api.CloudBackupCatalogRequest{
ID: req.GetBackupId(),
CredentialUUID: req.GetCredentialId(),
CredentialUUID: credId,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to get catalog: %v", err)
Expand Down Expand Up @@ -334,12 +373,17 @@ func (s *CloudBackupServer) SchedCreate(
return nil, status.Error(codes.Unavailable, "Resource has not been initialized")
}

credId := req.GetCloudSchedInfo().GetCredentialId()
var err error
if req.GetCloudSchedInfo() == nil {
return nil, status.Error(codes.InvalidArgument, "BackupSchedule object cannot be nil")
} else if len(req.GetCloudSchedInfo().GetSrcVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must supply source volume id")
} else if len(req.GetCloudSchedInfo().GetCredentialId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must supply credential uuid")
credId, err = s.defaultCloudBackupCreds(ctx)
if err != nil {
return nil, err
}
} else if req.GetCloudSchedInfo().GetSchedules() == nil ||
len(req.GetCloudSchedInfo().GetSchedules()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Must supply Schedule")
Expand All @@ -349,8 +393,10 @@ func (s *CloudBackupServer) SchedCreate(
if err := checkAccessFromDriverForVolumeId(ctx, s.driver(ctx), req.GetCloudSchedInfo().GetSrcVolumeId(), api.Ownership_Read); err != nil {
return nil, err
}
if err := s.checkAccessToCredential(ctx, req.GetCloudSchedInfo().GetCredentialId()); err != nil {
return nil, err
if len(req.GetCloudSchedInfo().GetCredentialId()) != 0 {
if err := s.checkAccessToCredential(ctx, req.GetCloudSchedInfo().GetCredentialId()); err != nil {
return nil, err
}
}

sched, err := sdkSchedToRetainInternalSpecYamlByte(req.GetCloudSchedInfo().GetSchedules())
Expand All @@ -360,7 +406,7 @@ func (s *CloudBackupServer) SchedCreate(

bkpRequest := api.CloudBackupSchedCreateRequest{}
bkpRequest.SrcVolumeID = req.GetCloudSchedInfo().GetSrcVolumeId()
bkpRequest.CredentialUUID = req.GetCloudSchedInfo().GetCredentialId()
bkpRequest.CredentialUUID = credId
bkpRequest.Schedule = string(sched)
bkpRequest.MaxBackups = uint(req.GetCloudSchedInfo().GetMaxBackups())
bkpRequest.Full = req.GetCloudSchedInfo().GetFull()
Expand Down Expand Up @@ -462,3 +508,21 @@ func ToSdkCloudBackupdScheduleInfo(s api.CloudBackupScheduleInfo) *api.SdkCloudB
}
return cloudSched
}

func (s *CloudBackupServer) defaultCloudBackupCreds(
ctx context.Context,
) (string, error) {

req := &api.SdkCredentialEnumerateRequest{}
cs := &CredentialServer{server: s.server}
credList, err := cs.Enumerate(ctx, req)
if err != nil {
return "", err
}

if len(credList.CredentialIds) > 1 || len(credList.CredentialIds) == 0 {
return "", status.Error(codes.InvalidArgument, "Either no credential or more than one configured,"+
"please specify a credential name or uuid to use")
}
return credList.CredentialIds[0], nil
}
Loading

0 comments on commit ecea611

Please sign in to comment.