Skip to content

Commit

Permalink
Add async file upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
radito3 committed Aug 31, 2023
1 parent 58830c1 commit 3367c8d
Show file tree
Hide file tree
Showing 37 changed files with 675 additions and 786 deletions.
4 changes: 2 additions & 2 deletions clients/baseclient/client_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var _ = Describe("ClientUtil", func() {
toReturn := testStruct{}
return toReturn, nil
}
result, err := CallWithRetry(getInterface, 4, (time.Duration(0) * time.Second))
result, err := CallWithRetry(getInterface, 4, time.Duration(0))
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(result))
})
Expand All @@ -87,7 +87,7 @@ var _ = Describe("ClientUtil", func() {
err := ClientError{404, "Not Found", "The server cannot find requesred resource"}
return toReturn, &err
}
result, err := CallWithRetry(mockCallback, 4, (time.Duration(0) * time.Second))
result, err := CallWithRetry(mockCallback, 4, time.Duration(0))
Expect(err).To(HaveOccurred())
Expect(result).To(Equal(result))
})
Expand Down
4 changes: 4 additions & 0 deletions clients/baseclient/test_token_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ type customTokenfactory struct {
func (c *customTokenfactory) NewToken() (runtime.ClientAuthInfoWriter, error) {
return testutil.NewCustomBearerToken(c.tokenString), nil
}

func (c *customTokenfactory) NewRawToken() (string, error) {
return c.tokenString, nil
}
1 change: 1 addition & 0 deletions clients/baseclient/token_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "github.com/go-openapi/runtime"
// TokenFactory factory for generating new OAuth token
type TokenFactory interface {
NewToken() (runtime.ClientAuthInfoWriter, error)
NewRawToken() (string, error)
}
31 changes: 18 additions & 13 deletions clients/csrf/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package csrf

import (
"net/http"
"strconv"
"strings"

"github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
"github.com/jinzhu/copier"
)

type Csrf struct {
Expand All @@ -26,18 +25,24 @@ type Transport struct {
}

func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
req2 := http.Request{}
copier.Copy(&req2, req)
//TODO rework this to be like so:
// get the auth header from the outbound request
// call the csrf endpoint or use a cached token
// add the csrf header
// ---
// that way, we avoid creating and using a cli connection again

csrfTokenManager := NewDefaultCsrfTokenManager(&t, &req2)
reqCopy := req.Clone(req.Context())

csrfTokenManager := NewDefaultCsrfTokenManager(&t, reqCopy)
err := csrfTokenManager.updateToken()
if err != nil {
return nil, err
}

log.Tracef("Sending a request with CSRF '" + req2.Header.Get("X-Csrf-Header") + " : " + req2.Header.Get("X-Csrf-Token") + "'\n")
log.Tracef("Cookies used are: " + prettyPrintCookies(req2.Cookies()) + "\n")
res, err := t.OriginalTransport.RoundTrip(&req2)
log.Tracef("Sending a request with CSRF '%s : %s'\n", reqCopy.Header.Get("X-Csrf-Header"), reqCopy.Header.Get("X-Csrf-Token"))
log.Tracef("Cookies used are: %s\n", prettyPrintCookies(reqCopy.Cookies()))
res, err := t.OriginalTransport.RoundTrip(reqCopy)
if err != nil {
return nil, err
}
Expand All @@ -47,17 +52,17 @@ func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
}

if tokenWasRefreshed {
log.Tracef("Response code '" + strconv.Itoa(res.StatusCode) + "' from bad token. Must Retry.\n")
log.Tracef("Response code '%d' from bad token. Must Retry.\n", res.StatusCode)
return nil, &ForbiddenError{}
}

return res, err
}

func prettyPrintCookies(cookies []*http.Cookie) string {
result := ""
var result strings.Builder
for _, cookie := range cookies {
result = result + cookie.String() + " "
result.WriteString(cookie.String())
result.WriteRune(' ')
}
return result
return result.String()
}
17 changes: 11 additions & 6 deletions clients/mtaclient/fakes/fake_mta_client_builder.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package fakes

import (
"net/http"
"os"

"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient"
)

type FakeMtaClientBuilder struct {
FakeMtaClient FakeMtaClientOperations
FakeMtaClient *FakeMtaClientOperations
}

func NewFakeMtaClientBuilder() *FakeMtaClientBuilder {
return &FakeMtaClientBuilder{}
return &FakeMtaClientBuilder{&FakeMtaClientOperations{}}
}

func (fb *FakeMtaClientBuilder) ExecuteAction(operationID, actionID string, resultHeader mtaclient.ResponseHeader, resultError error) *FakeMtaClientBuilder {
Expand Down Expand Up @@ -51,19 +52,23 @@ func (fb *FakeMtaClientBuilder) StartMtaOperation(operation models.Operation, re
fb.FakeMtaClient.StartMtaOperationReturns(result, resultError)
return fb
}
func (fb *FakeMtaClientBuilder) UploadMtaFile(file os.File, result *models.FileMetadata, resultError error) *FakeMtaClientBuilder {
func (fb *FakeMtaClientBuilder) UploadMtaFile(file *os.File, result *models.FileMetadata, resultError error) *FakeMtaClientBuilder {
fb.FakeMtaClient.UploadMtaFileReturns(result, resultError)
return fb
}
func (fb *FakeMtaClientBuilder) UploadMtaArchiveFromUrl(fileUrl string, result *models.FileMetadata, resultError error) *FakeMtaClientBuilder {
fb.FakeMtaClient.UploadMtaArchiveFromUrlReturnsOnCall(fileUrl, result, resultError)
func (fb *FakeMtaClientBuilder) StartUploadMtaArchiveFromUrl(fileUrl string, namespace *string, result http.Header, resultError error) *FakeMtaClientBuilder {
fb.FakeMtaClient.StartUploadMtaArchiveFromUrlReturnsOnCall(fileUrl, namespace, result, resultError)
return fb
}
func (fb *FakeMtaClientBuilder) GetAsyncUploadJob(jobId string, namespace *string, appInstanceId string, result mtaclient.AsyncUploadJobResult, resultErr error) *FakeMtaClientBuilder {
fb.FakeMtaClient.GetAsyncUploadJobReturnsOnCall(jobId, namespace, appInstanceId, result, resultErr)
return fb
}
func (fb *FakeMtaClientBuilder) GetMtaOperationLogContent(operationID, logID string, result string, resultError error) *FakeMtaClientBuilder {
fb.FakeMtaClient.GetMtaOperationLogContentReturns(result, resultError)
return fb
}

func (fb *FakeMtaClientBuilder) Build() FakeMtaClientOperations {
func (fb *FakeMtaClientBuilder) Build() *FakeMtaClientOperations {
return fb.FakeMtaClient
}
123 changes: 78 additions & 45 deletions clients/mtaclient/fakes/fake_mta_client_operations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package fakes

import (
"os"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/util"
"net/http"
"sync"

"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
Expand Down Expand Up @@ -126,10 +127,10 @@ type FakeMtaClientOperations struct {
result1 mtaclient.ResponseHeader
result2 error
}
UploadMtaFileStub func(file os.File) (*models.FileMetadata, error)
UploadMtaFileStub func(file util.NamedReadSeeker) (*models.FileMetadata, error)
uploadMtaFileMutex sync.RWMutex
uploadMtaFileArgsForCall []struct {
file os.File
file util.NamedReadSeeker
}
uploadMtaFileReturns struct {
result1 *models.FileMetadata
Expand All @@ -139,14 +140,23 @@ type FakeMtaClientOperations struct {
result1 *models.FileMetadata
result2 error
}
uploadMtaArchiveFromUrlMutex sync.RWMutex
uploadMtaArchiveFromUrlReturns struct {
result1 *models.FileMetadata
startUploadMtaArchiveFromUrlMutex sync.RWMutex
startUploadMtaArchiveFromUrlReturns struct {
result1 http.Header
result2 error
}
startUploadMtaArchiveFromUrlReturnsOnCall map[string]struct {
headers http.Header
err error
}
getAsyncUploadJobMutex sync.RWMutex
getAsyncUploadJobReturns struct {
result1 mtaclient.AsyncUploadJobResult
result2 error
}
uploadMtaArchiveFromUrlReturnsOnCall map[string]struct {
file *models.FileMetadata
err error
getAsyncUploadJobReturnsOnCall map[string]struct {
result mtaclient.AsyncUploadJobResult
err error
}
GetMtaOperationLogContentStub func(operationID, logID string) (string, error)
getMtaOperationLogContentMutex sync.RWMutex
Expand All @@ -166,7 +176,7 @@ type FakeMtaClientOperations struct {
invocationsMutex sync.RWMutex
}

func (fake FakeMtaClientOperations) ExecuteAction(operationID string, actionID string) (mtaclient.ResponseHeader, error) {
func (fake *FakeMtaClientOperations) ExecuteAction(operationID string, actionID string) (mtaclient.ResponseHeader, error) {
fake.executeActionMutex.Lock()
ret, specificReturn := fake.executeActionReturnsOnCall[len(fake.executeActionArgsForCall)]
fake.executeActionArgsForCall = append(fake.executeActionArgsForCall, struct {
Expand Down Expand Up @@ -218,7 +228,7 @@ func (fake *FakeMtaClientOperations) ExecuteActionReturnsOnCall(i int, result1 m
}{result1, result2}
}

func (fake FakeMtaClientOperations) GetMta(mtaID string) (*models.Mta, error) {
func (fake *FakeMtaClientOperations) GetMta(mtaID string) (*models.Mta, error) {
fake.getMtaMutex.Lock()
ret, specificReturn := fake.getMtaReturnsOnCall[len(fake.getMtaArgsForCall)]
fake.getMtaArgsForCall = append(fake.getMtaArgsForCall, struct {
Expand Down Expand Up @@ -269,7 +279,7 @@ func (fake *FakeMtaClientOperations) GetMtaReturnsOnCall(i int, result1 *models.
}{result1, result2}
}

func (fake FakeMtaClientOperations) GetMtaFiles(namespace *string) ([]*models.FileMetadata, error) {
func (fake *FakeMtaClientOperations) GetMtaFiles(namespace *string) ([]*models.FileMetadata, error) {
fake.getMtaFilesMutex.Lock()
ret, specificReturn := fake.getMtaFilesReturnsOnCall[len(fake.getMtaFilesArgsForCall)]
fake.getMtaFilesArgsForCall = append(fake.getMtaFilesArgsForCall, struct{}{})
Expand Down Expand Up @@ -312,7 +322,7 @@ func (fake *FakeMtaClientOperations) GetMtaFilesReturnsOnCall(i int, result1 []*
}{result1, result2}
}

func (fake FakeMtaClientOperations) GetMtaOperation(operationID string, embed string) (*models.Operation, error) {
func (fake *FakeMtaClientOperations) GetMtaOperation(operationID string, embed string) (*models.Operation, error) {
fake.getMtaOperationMutex.Lock()
ret, specificReturn := fake.getMtaOperationReturnsOnCall[len(fake.getMtaOperationArgsForCall)]
fake.getMtaOperationArgsForCall = append(fake.getMtaOperationArgsForCall, struct {
Expand Down Expand Up @@ -364,7 +374,7 @@ func (fake *FakeMtaClientOperations) GetMtaOperationReturnsOnCall(i int, result1
}{result1, result2}
}

func (fake FakeMtaClientOperations) GetMtaOperationLogs(operationID string) ([]*models.Log, error) {
func (fake *FakeMtaClientOperations) GetMtaOperationLogs(operationID string) ([]*models.Log, error) {
fake.getMtaOperationLogsMutex.Lock()
ret, specificReturn := fake.getMtaOperationLogsReturnsOnCall[len(fake.getMtaOperationLogsArgsForCall)]
fake.getMtaOperationLogsArgsForCall = append(fake.getMtaOperationLogsArgsForCall, struct {
Expand Down Expand Up @@ -415,7 +425,7 @@ func (fake *FakeMtaClientOperations) GetMtaOperationLogsReturnsOnCall(i int, res
}{result1, result2}
}

func (fake FakeMtaClientOperations) GetMtaOperations(mtaId *string, last *int64, status []string) ([]*models.Operation, error) {
func (fake *FakeMtaClientOperations) GetMtaOperations(mtaId *string, last *int64, status []string) ([]*models.Operation, error) {
var statusCopy []string
if status != nil {
statusCopy = make([]string, len(status))
Expand Down Expand Up @@ -473,7 +483,7 @@ func (fake *FakeMtaClientOperations) GetMtaOperationsReturnsOnCall(i int, result
}{result1, result2}
}

func (fake FakeMtaClientOperations) GetMtas() ([]*models.Mta, error) {
func (fake *FakeMtaClientOperations) GetMtas() ([]*models.Mta, error) {
fake.getMtasMutex.Lock()
ret, specificReturn := fake.getMtasReturnsOnCall[len(fake.getMtasArgsForCall)]
fake.getMtasArgsForCall = append(fake.getMtasArgsForCall, struct{}{})
Expand Down Expand Up @@ -516,7 +526,7 @@ func (fake *FakeMtaClientOperations) GetMtasReturnsOnCall(i int, result1 []*mode
}{result1, result2}
}

func (fake FakeMtaClientOperations) GetOperationActions(operationID string) ([]string, error) {
func (fake *FakeMtaClientOperations) GetOperationActions(operationID string) ([]string, error) {
fake.getOperationActionsMutex.Lock()
ret, specificReturn := fake.getOperationActionsReturnsOnCall[len(fake.getOperationActionsArgsForCall)]
fake.getOperationActionsArgsForCall = append(fake.getOperationActionsArgsForCall, struct {
Expand Down Expand Up @@ -567,7 +577,7 @@ func (fake *FakeMtaClientOperations) GetOperationActionsReturnsOnCall(i int, res
}{result1, result2}
}

func (fake FakeMtaClientOperations) StartMtaOperation(operation models.Operation) (mtaclient.ResponseHeader, error) {
func (fake *FakeMtaClientOperations) StartMtaOperation(operation models.Operation) (mtaclient.ResponseHeader, error) {
fake.startMtaOperationMutex.Lock()
ret, specificReturn := fake.startMtaOperationReturnsOnCall[len(fake.startMtaOperationArgsForCall)]
fake.startMtaOperationArgsForCall = append(fake.startMtaOperationArgsForCall, struct {
Expand Down Expand Up @@ -618,11 +628,11 @@ func (fake *FakeMtaClientOperations) StartMtaOperationReturnsOnCall(i int, resul
}{result1, result2}
}

func (fake FakeMtaClientOperations) UploadMtaFile(file os.File, fileSize int64, namespace *string) (*models.FileMetadata, error) {
func (fake *FakeMtaClientOperations) UploadMtaFile(file util.NamedReadSeeker, fileSize int64, namespace *string) (*models.FileMetadata, error) {
fake.uploadMtaFileMutex.Lock()
ret, specificReturn := fake.uploadMtaFileReturnsOnCall[len(fake.uploadMtaFileArgsForCall)]
fake.uploadMtaFileArgsForCall = append(fake.uploadMtaFileArgsForCall, struct {
file os.File
file util.NamedReadSeeker
}{file})
fake.recordInvocation("UploadMtaFile", []interface{}{file})
fake.uploadMtaFileMutex.Unlock()
Expand All @@ -641,7 +651,7 @@ func (fake *FakeMtaClientOperations) UploadMtaFileCallCount() int {
return len(fake.uploadMtaFileArgsForCall)
}

func (fake *FakeMtaClientOperations) UploadMtaFileArgsForCall(i int) os.File {
func (fake *FakeMtaClientOperations) UploadMtaFileArgsForCall(i int) util.NamedReadSeeker {
fake.uploadMtaFileMutex.RLock()
defer fake.uploadMtaFileMutex.RUnlock()
return fake.uploadMtaFileArgsForCall[i].file
Expand Down Expand Up @@ -669,38 +679,61 @@ func (fake *FakeMtaClientOperations) UploadMtaFileReturnsOnCall(i int, result1 *
}{result1, result2}
}

func (fake FakeMtaClientOperations) UploadMtaArchiveFromUrl(fileUrl string, namespace *string) (*models.FileMetadata, error) {
fake.uploadMtaArchiveFromUrlMutex.Lock()
result, specificReturn := fake.uploadMtaArchiveFromUrlReturnsOnCall[fileUrl]
fake.recordInvocation("UploadMtaArchiveFromUrl", []interface{}{fileUrl})
fake.uploadMtaArchiveFromUrlMutex.Unlock()
func (fake *FakeMtaClientOperations) StartUploadMtaArchiveFromUrl(fileUrl string, namespace *string) (http.Header, error) {
fake.startUploadMtaArchiveFromUrlMutex.Lock()
result, specificReturn := fake.startUploadMtaArchiveFromUrlReturnsOnCall[fileUrl]
fake.recordInvocation("StartUploadMtaArchiveFromUrl", []interface{}{fileUrl, namespace})
fake.startUploadMtaArchiveFromUrlMutex.Unlock()
if specificReturn {
return result.file, result.err
return result.headers, result.err
}
return fake.uploadMtaArchiveFromUrlReturns.result1, fake.uploadMtaArchiveFromUrlReturns.result2
return fake.startUploadMtaArchiveFromUrlReturns.result1, fake.startUploadMtaArchiveFromUrlReturns.result2
}

func (fake *FakeMtaClientOperations) UploadMtaArchiveFromUrlReturns(result1 *models.FileMetadata, result2 error) {
fake.uploadMtaArchiveFromUrlReturns = struct {
result1 *models.FileMetadata
result2 error
}{result1, result2}
func (fake *FakeMtaClientOperations) StartUploadMtaArchiveFromUrlReturnsOnCall(fileUrl string, namespace *string, headers http.Header, err error) {
fake.startUploadMtaArchiveFromUrlMutex.Lock()
if fake.startUploadMtaArchiveFromUrlReturnsOnCall == nil {
fake.startUploadMtaArchiveFromUrlReturnsOnCall = make(map[string]struct {
headers http.Header
err error
})
}
fake.startUploadMtaArchiveFromUrlReturnsOnCall[fileUrl] = struct {
headers http.Header
err error
}{headers, err}
fake.recordInvocation("StartUploadMtaArchiveFromUrl", []interface{}{fileUrl, namespace})
fake.startUploadMtaArchiveFromUrlMutex.Unlock()
}

func (fake *FakeMtaClientOperations) UploadMtaArchiveFromUrlReturnsOnCall(fileUrl string, result1 *models.FileMetadata, result2 error) {
if fake.uploadMtaArchiveFromUrlReturnsOnCall == nil {
fake.uploadMtaArchiveFromUrlReturnsOnCall = make(map[string]struct {
file *models.FileMetadata
err error
func (fake *FakeMtaClientOperations) GetAsyncUploadJob(jobId string, namespace *string, appInstanceId string) (mtaclient.AsyncUploadJobResult, error) {
fake.getAsyncUploadJobMutex.Lock()
result, specificReturn := fake.getAsyncUploadJobReturnsOnCall[jobId]
fake.recordInvocation("GetAsyncUploadJob", []interface{}{jobId, namespace, appInstanceId})
fake.getAsyncUploadJobMutex.Unlock()
if specificReturn {
return result.result, result.err
}
return fake.getAsyncUploadJobReturns.result1, fake.startUploadMtaArchiveFromUrlReturns.result2
}

func (fake *FakeMtaClientOperations) GetAsyncUploadJobReturnsOnCall(jobId string, namespace *string, appInstanceId string, result mtaclient.AsyncUploadJobResult, err error) {
fake.getAsyncUploadJobMutex.Lock()
if fake.getAsyncUploadJobReturnsOnCall == nil {
fake.getAsyncUploadJobReturnsOnCall = make(map[string]struct {
result mtaclient.AsyncUploadJobResult
err error
})
}
fake.uploadMtaArchiveFromUrlReturnsOnCall[fileUrl] = struct {
file *models.FileMetadata
err error
}{result1, result2}
fake.getAsyncUploadJobReturnsOnCall[jobId] = struct {
result mtaclient.AsyncUploadJobResult
err error
}{result, err}
fake.recordInvocation("GetAsyncUploadJob", []interface{}{jobId, namespace, appInstanceId})
fake.getAsyncUploadJobMutex.Unlock()
}

func (fake FakeMtaClientOperations) GetMtaOperationLogContent(operationID string, logID string) (string, error) {
func (fake *FakeMtaClientOperations) GetMtaOperationLogContent(operationID string, logID string) (string, error) {
fake.getMtaOperationLogContentMutex.Lock()
ret, specificReturn := fake.getMtaOperationLogContentReturnsOnCall[len(fake.getMtaOperationLogContentArgsForCall)]
fake.getMtaOperationLogContentArgsForCall = append(fake.getMtaOperationLogContentArgsForCall, struct {
Expand Down Expand Up @@ -775,8 +808,8 @@ func (fake *FakeMtaClientOperations) Invocations() map[string][][]interface{} {
defer fake.startMtaOperationMutex.RUnlock()
fake.uploadMtaFileMutex.RLock()
defer fake.uploadMtaFileMutex.RUnlock()
fake.uploadMtaArchiveFromUrlMutex.RLock()
defer fake.uploadMtaArchiveFromUrlMutex.RUnlock()
fake.startUploadMtaArchiveFromUrlMutex.RLock()
defer fake.startUploadMtaArchiveFromUrlMutex.RUnlock()
fake.getMtaOperationLogContentMutex.RLock()
defer fake.getMtaOperationLogContentMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
Expand Down
Loading

0 comments on commit 3367c8d

Please sign in to comment.