CloudMigrations: Query GMS for a presigned upload url at upload time (#90505)
query GMS for an upload url at upload time
This commit is contained in:
@@ -11,6 +11,7 @@ type Client interface {
|
||||
MigrateData(context.Context, cloudmigration.CloudMigrationSession, cloudmigration.MigrateDataRequest) (*cloudmigration.MigrateDataResponse, error)
|
||||
StartSnapshot(context.Context, cloudmigration.CloudMigrationSession) (*cloudmigration.StartSnapshotResponse, error)
|
||||
GetSnapshotStatus(context.Context, cloudmigration.CloudMigrationSession, cloudmigration.CloudMigrationSnapshot, int) (*cloudmigration.GetSnapshotStatusResponse, error)
|
||||
CreatePresignedUploadUrl(context.Context, cloudmigration.CloudMigrationSession, cloudmigration.CloudMigrationSnapshot) (string, error)
|
||||
}
|
||||
|
||||
const logPrefix = "cloudmigration.gmsclient"
|
||||
|
||||
@@ -44,3 +44,7 @@ type MigrateDataResponseItemDTO struct {
|
||||
Status ItemStatus `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type CreateSnapshotUploadUrlResponseDTO struct {
|
||||
UploadUrl string `json:"uploadUrl"`
|
||||
}
|
||||
|
||||
@@ -206,6 +206,47 @@ func (c *gmsClientImpl) GetSnapshotStatus(ctx context.Context, session cloudmigr
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (c *gmsClientImpl) CreatePresignedUploadUrl(ctx context.Context, session cloudmigration.CloudMigrationSession, snapshot cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
logger := c.log.FromContext(ctx)
|
||||
|
||||
path := fmt.Sprintf("%s/api/v1/status/%s/create-upload-url", c.buildBasePath(session.ClusterSlug), snapshot.GMSSnapshotUID)
|
||||
|
||||
// Send the request to gms with the associated auth token
|
||||
req, err := http.NewRequest(http.MethodPost, path, nil)
|
||||
if err != nil {
|
||||
c.log.Error("error creating http request to create upload url", "err", err.Error())
|
||||
return "", fmt.Errorf("http request error: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %d:%s", session.StackID, session.AuthToken))
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: c.cfg.CloudMigration.GMSCreateUploadUrlTimeout,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
c.log.Error("error sending http request to create an upload url", "err", err.Error())
|
||||
return "", fmt.Errorf("http request error: %w", err)
|
||||
} else if resp.StatusCode >= 400 {
|
||||
c.log.Error("received error response to create an upload url", "statusCode", resp.StatusCode)
|
||||
return "", fmt.Errorf("http request error: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
logger.Error("closing request body: %w", err)
|
||||
}
|
||||
}()
|
||||
|
||||
var result CreateSnapshotUploadUrlResponseDTO
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
logger.Error("unmarshalling response body: %w", err)
|
||||
return "", fmt.Errorf("unmarshalling create upload url response: %w", err)
|
||||
}
|
||||
|
||||
return result.UploadUrl, nil
|
||||
}
|
||||
|
||||
func (c *gmsClientImpl) buildBasePath(clusterSlug string) string {
|
||||
domain := c.cfg.CloudMigration.GMSDomain
|
||||
if strings.HasPrefix(domain, "http://localhost") {
|
||||
|
||||
@@ -58,7 +58,6 @@ func (c *memoryClientImpl) StartSnapshot(context.Context, cloudmigration.CloudMi
|
||||
}
|
||||
c.snapshot = &cloudmigration.StartSnapshotResponse{
|
||||
EncryptionKey: fmt.Sprintf("%x", publicKey[:]),
|
||||
UploadURL: "localhost:3000",
|
||||
SnapshotID: uuid.NewString(),
|
||||
MaxItemsPerPartition: 10,
|
||||
Algo: "nacl",
|
||||
@@ -92,3 +91,7 @@ func (c *memoryClientImpl) GetSnapshotStatus(ctx context.Context, session cloudm
|
||||
|
||||
return gmsResp, nil
|
||||
}
|
||||
|
||||
func (c *memoryClientImpl) CreatePresignedUploadUrl(ctx context.Context, sess cloudmigration.CloudMigrationSession, snapshot cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
return "http://localhost:3000", nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user