Skip to content

Commit

Permalink
rename Wait to WaitHandler (#150)
Browse files Browse the repository at this point in the history
clone client for each services

Co-authored-by: Dean Oren <deangili.oren@mail.schwarz>
  • Loading branch information
do87 and Dean Oren committed Mar 28, 2023
1 parent bbc4634 commit 6944bb4
Show file tree
Hide file tree
Showing 24 changed files with 107 additions and 61 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewClientWithKeyAuth(ctx context.Context, cfg ...clients.KeyFlowConfig) (*s
if err := client.Init(ctx, cfg...); err != nil {
return nil, err
}
return services.Init[clients.KeyFlowConfig](client), nil
return services.Init(client)
}

// MustNewClientWithKeyAuth panics if client initialization failed
Expand All @@ -34,7 +34,7 @@ func NewClientWithTokenAuth(ctx context.Context, cfg ...clients.TokenFlowConfig)
if err := client.Init(ctx, cfg...); err != nil {
return nil, err
}
return services.Init[clients.TokenFlowConfig](client), nil
return services.Init(client)
}

// MustNewClientWithTokenAuth panics if client initialization failed
Expand Down
2 changes: 1 addition & 1 deletion examples/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
panic(err)
}

process := res.Wait(ctx, bucket, projectID, bucketName)
process := res.WaitHandler(ctx, bucket, projectID, bucketName)
if _, err := process.WaitWithContext(ctx); err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/config/argus/v1.0/include/instances/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (

// Wait will wait for creation
// returned interface is nil or *instances.ProjectInstanceUI
func (*CreateResponse) Wait(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200"); agg != nil {
Expand All @@ -46,7 +46,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *instances.ClientWithResponse

// Wait will wait for update
// returned interface is nil or *instances.ProjectInstanceUI
func (*UpdateResponse) Wait(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*UpdateResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
seenUpdating := false
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
Expand Down Expand Up @@ -112,7 +112,7 @@ func (*UpdateResponse) Wait(ctx context.Context, c *instances.ClientWithResponse

// Wait will wait for deletion
// returned interface is nil
func (*DeleteResponse) Wait(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200"); agg != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/config/data-services/v1.0/include/instances/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
client_timeout_err = "Client.Timeout exceeded while awaiting headers"
)

func (ProvisionResponse) Wait(ctx context.Context, c *instances.ClientWithResponses, r ProvisionResponse, projectID, instanceID string) *wait.Handler {
func (ProvisionResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, r ProvisionResponse, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if err = validate.Response(s, err, "JSON200"); err != nil {
Expand All @@ -39,7 +39,7 @@ func (ProvisionResponse) Wait(ctx context.Context, c *instances.ClientWithRespon
})
}

func (UpdateResponse) Wait(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (UpdateResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if err = validate.Response(s, err, "JSON200"); err != nil {
Expand All @@ -66,7 +66,7 @@ func (UpdateResponse) Wait(ctx context.Context, c *instances.ClientWithResponses
})
}

func (DeprovisionResponse) Wait(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (DeprovisionResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if err = validate.Response(s, err, "JSON200"); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/kubernetes/v1.0/include/cluster/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)

func (*CreateOrUpdateResponse) Wait(ctx context.Context, c *cluster.ClientWithResponses, projectID, clusterName string) *wait.Handler {
func (*CreateOrUpdateResponse) WaitHandler(ctx context.Context, c *cluster.ClientWithResponses, projectID, clusterName string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.Get(ctx, projectID, clusterName)
if err != nil {
Expand All @@ -36,7 +36,7 @@ func (*CreateOrUpdateResponse) Wait(ctx context.Context, c *cluster.ClientWithRe
})
}

func (*DeleteResponse) Wait(ctx context.Context, c *cluster.ClientWithResponses, projectID, clusterName string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *cluster.ClientWithResponses, projectID, clusterName string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.Get(ctx, projectID, clusterName)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/kubernetes/v1.0/include/project/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"
)

func (*CreateResponse) Wait(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {

resp, err := c.Get(ctx, projectID)
Expand Down Expand Up @@ -40,7 +40,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *project.ClientWithResponses,
})
}

func (*DeleteResponse) Wait(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.Get(ctx, projectID)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/mongodb-flex/v1.0/include/instance/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ClientTimeoutErr = "Client.Timeout exceeded while awaiting headers"

// Wait will wait for instance update to complete
// returned interface is nil
func (*CreateResponse) Wait(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
// artificial wait for instance to change status
time.Sleep(5 * time.Second)

Expand Down Expand Up @@ -55,7 +55,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *instance.ClientWithResponses

// WaitHandler will wait for instance deletion
// returned value for deletion wait will always be nil
func (DeleteResponse) Wait(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (DeleteResponse) WaitHandler(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
s, err := c.List(ctx, projectID, &instance.ListParams{})
if err = validate.Response(s, err, "JSON200.Items"); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/object-storage/v1.0.1/include/bucket/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Wait waits for creation. in case there are no errors, the returned interface is of *GetResponse
func (*CreateResponse) Wait(ctx context.Context, c *bucket.ClientWithResponses, projectID, bucketName string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *bucket.ClientWithResponses, projectID, bucketName string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
res, err := c.Get(ctx, projectID, bucketName)
if err = validate.Response(res, err); err != nil {
Expand All @@ -29,7 +29,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *bucket.ClientWithResponses,
}

// Wait waits for deletion
func (*DeleteResponse) Wait(ctx context.Context, c *bucket.ClientWithResponses, projectID, bucketName string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *bucket.ClientWithResponses, projectID, bucketName string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
res, err := c.Get(ctx, projectID, bucketName)
if err = validate.Response(res, err); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/config/postgres-flex/v1.0/include/instance/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
)

// Wait will wait for instance create to complete
func (*CreateResponse) Wait(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return waitForCreateOrUpdate(ctx, c, projectID, instanceID)
}

// Wait will wait for instance update to complete
func (*PutResponse) Wait(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*PutResponse) WaitHandler(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return waitForCreateOrUpdate(ctx, c, projectID, instanceID)
}

// Wait will wait for instance update to complete
func (*PatchResponse) Wait(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*PatchResponse) WaitHandler(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return waitForCreateOrUpdate(ctx, c, projectID, instanceID)
}

Expand Down Expand Up @@ -52,7 +52,7 @@ func waitForCreateOrUpdate(ctx context.Context, c *instance.ClientWithResponses,

// Wait will wait for instance deletion
// returned value for deletion wait will always be nil
func (DeleteResponse) Wait(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (DeleteResponse) WaitHandler(ctx context.Context, c *instance.ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
res, err := c.Get(ctx, projectID, instanceID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/postgres-flex/v1.0/include/users/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ClientTimeoutErr = "Client.Timeout exceeded while awaiting headers"

// Wait will wait for user deletion
// returned value for deletion wait will always be nil
func (*DeleteResponse) Wait(ctx context.Context, c *users.ClientWithResponses, projectID, instanceID, userID string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *users.ClientWithResponses, projectID, instanceID, userID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
s, err := c.List(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200.Items"); agg != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/resource-management/v2.0/include/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)

func (*CreateResponse) Wait(ctx context.Context, c *resourcemanagement.ClientWithResponses, containerID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *resourcemanagement.ClientWithResponses, containerID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
project, err := c.Get(ctx, containerID, &resourcemanagement.GetParams{})
if err != nil {
Expand All @@ -36,7 +36,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *resourcemanagement.ClientWit
})
}

func (*DeleteResponse) Wait(ctx context.Context, c *resourcemanagement.ClientWithResponses, containerID string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *resourcemanagement.ClientWithResponses, containerID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
project, err := c.Get(ctx, containerID, &resourcemanagement.GetParams{})
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/clients/key_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ func (c *KeyFlow) Init(ctx context.Context, cfg ...KeyFlowConfig) error {
return nil
}

// Clone creates a clone of the client
func (c *KeyFlow) Clone() interface{} {
sc := *c
nc := &sc
cl := *nc.client
cf := *nc.config
ke := *nc.key
to := *nc.token
nc.client = &cl
nc.config = &cf
nc.key = &ke
nc.token = &to
return c
}

// Do performs the reuqest
func (c *KeyFlow) Do(req *http.Request) (*http.Response, error) {
accessToken, err := c.GetAccessToken()
Expand Down
11 changes: 11 additions & 0 deletions pkg/clients/token_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ func (c *TokenFlow) Init(ctx context.Context, cfg ...TokenFlowConfig) error {
return c.validate()
}

// Clone creates a clone of the client
func (c *TokenFlow) Clone() interface{} {
sc := *c
nc := &sc
cl := *nc.client
cf := *nc.config
nc.client = &cl
nc.config = &cf
return c
}

// processConfig processes the given configuration
func (c *TokenFlow) processConfig(cfg ...TokenFlowConfig) {
c.config = c.getConfigFromEnvironment()
Expand Down
1 change: 1 addition & 0 deletions pkg/contracts/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ type BaseClientInterface interface {
Do(req *http.Request) (*http.Response, error)
GetEnvironment() env.Environment
GetServiceAccountEmail() string
Clone() interface{}
}
6 changes: 3 additions & 3 deletions pkg/services/argus/v1.0/instances/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

// Wait will wait for creation
// returned interface is nil or *ProjectInstanceUI
func (*CreateResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200"); agg != nil {
Expand All @@ -45,7 +45,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *ClientWithResponses, project

// Wait will wait for update
// returned interface is nil or *ProjectInstanceUI
func (*UpdateResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*UpdateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
seenUpdating := false
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
Expand Down Expand Up @@ -111,7 +111,7 @@ func (*UpdateResponse) Wait(ctx context.Context, c *ClientWithResponses, project

// Wait will wait for deletion
// returned interface is nil
func (*DeleteResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if agg := validate.Response(s, err, "JSON200"); agg != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/services/data-services/v1.0/instances/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
client_timeout_err = "Client.Timeout exceeded while awaiting headers"
)

func (ProvisionResponse) Wait(ctx context.Context, c *ClientWithResponses, r ProvisionResponse, projectID, instanceID string) *wait.Handler {
func (ProvisionResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, r ProvisionResponse, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if err = validate.Response(s, err, "JSON200"); err != nil {
Expand All @@ -38,7 +38,7 @@ func (ProvisionResponse) Wait(ctx context.Context, c *ClientWithResponses, r Pro
})
}

func (UpdateResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (UpdateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if err = validate.Response(s, err, "JSON200"); err != nil {
Expand All @@ -65,7 +65,7 @@ func (UpdateResponse) Wait(ctx context.Context, c *ClientWithResponses, projectI
})
}

func (DeprovisionResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (DeprovisionResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, instanceID)
if err = validate.Response(s, err, "JSON200"); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/kubernetes/v1.0/cluster/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)

func (*CreateOrUpdateResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, clusterName string) *wait.Handler {
func (*CreateOrUpdateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, clusterName string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.Get(ctx, projectID, clusterName)
if err != nil {
Expand All @@ -35,7 +35,7 @@ func (*CreateOrUpdateResponse) Wait(ctx context.Context, c *ClientWithResponses,
})
}

func (*DeleteResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, clusterName string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, clusterName string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.Get(ctx, projectID, clusterName)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/kubernetes/v1.0/project/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/pkg/errors"
)

func (*CreateResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {

resp, err := c.Get(ctx, projectID)
Expand Down Expand Up @@ -39,7 +39,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *ClientWithResponses, project
})
}

func (*DeleteResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID string) *wait.Handler {
func (*DeleteResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.Get(ctx, projectID)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/mongodb-flex/v1.0/instance/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ClientTimeoutErr = "Client.Timeout exceeded while awaiting headers"

// Wait will wait for instance update to complete
// returned interface is nil
func (*CreateResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (*CreateResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
// artificial wait for instance to change status
time.Sleep(5 * time.Second)

Expand Down Expand Up @@ -54,7 +54,7 @@ func (*CreateResponse) Wait(ctx context.Context, c *ClientWithResponses, project

// WaitHandler will wait for instance deletion
// returned value for deletion wait will always be nil
func (DeleteResponse) Wait(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
func (DeleteResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, instanceID string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
s, err := c.List(ctx, projectID, &ListParams{})
if err = validate.Response(s, err, "JSON200.Items"); err != nil {
Expand Down
Loading

0 comments on commit 6944bb4

Please sign in to comment.