Skip to content

Commit

Permalink
Add LB 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Oren committed Nov 27, 2023
1 parent 64aaf9d commit 0f3ca1f
Show file tree
Hide file tree
Showing 17 changed files with 3,359 additions and 3 deletions.
74 changes: 74 additions & 0 deletions internal/config/load-balancer/1.3.0/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package: loadbalancer
output: ../../../../pkg/services/load-balancer/1.3.0/loadbalancer.go
generate:
models: true
client: true
output-options:
custom-doer:
enabled: true
import: contracts "github.com/SchwarzIT/community-stackit-go-client/pkg/contracts"
name: "contracts.BaseClientInterface"
split-by-tags:
verbose: false
enabled: true
include:
- Project
- Instances
extend-response:
- field: Error
type: error
description: "Aggregated error"
apply-to: ["*"]
imports:
- "github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
- "github.com/SchwarzIT/community-stackit-go-client/pkg/services/load-balancer/1.3.0/instances"
- "github.com/SchwarzIT/community-stackit-go-client/pkg/services/load-balancer/1.3.0/project"
set: "validate.DefaultResponseErrorHandler(rsp)"
copy:
- from: include/service.go
to: service.go
tidy:
- replace: "loadbalancer."
all: true
- from: include/instances/wait.go
to: instances/wait.go
tidy:
- replace: "instances."
all: true
- from: include/project/wait.go
to: project/wait.go
tidy:
- replace: "project."
all: true
tidy:
verbose: false
functions:
- replace: Id
with: ID
all: true
- replace: APIServiceDisable
with: DisableProject
all: true
- replace: APIServiceEnable
with: EnableProject
all: true
- replace: APIServiceStatus
with: GetStatus
all: true
- replace: APIService
prefix: true
- replace: LoadBalancers
all: true
- replace: LoadBalancer
all: true
- replace: CreateInstances
with: Create
all: true
params:
- replace: Id
with: ID
all: true
schemas:
- replace: Id
with: ID
all: true
3 changes: 3 additions & 0 deletions internal/config/load-balancer/1.3.0/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package gen

//go:generate go run github.com/do87/stackit-client-generator/cmd/oapi-codegen@v0.0.3 -config config.yaml load-balancer.json
14 changes: 14 additions & 0 deletions internal/config/load-balancer/1.3.0/include/instances/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// this file is only used to prevent wait.go
// from showing errors

package instances

import instances "github.com/SchwarzIT/community-stackit-go-client/pkg/services/load-balancer/1beta.0.0/instances"

type CreateResponse struct {
instances.ClientWithResponsesInterface
}

type DeleteResponse struct {
instances.ClientWithResponsesInterface
}
66 changes: 66 additions & 0 deletions internal/config/load-balancer/1.3.0/include/instances/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package instances

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/SchwarzIT/community-stackit-go-client/pkg/services/load-balancer/1beta.0.0/instances"
"github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)

// Wait will wait for instance create to complete
func (*CreateResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, name string) *wait.Handler {
maxFailCount := 10
return wait.New(func() (res interface{}, done bool, err error) {
s, err := c.Get(ctx, projectID, name)
if err = validate.Response(s, err, "JSON200.Status"); err != nil {
return nil, false, err
}
status := *s.JSON200.Status
if status == instances.STATUS_READY {
return s, true, nil
}
if status == instances.STATUS_ERROR {
if maxFailCount == 0 {
errorCollection := ""
if s != nil && s.JSON200 != nil && s.JSON200.Errors != nil {
for _, e := range *s.JSON200.Errors {
etype, edesc := "", ""
if e.Type != nil {
etype = string(*e.Type)
}
if e.Description != nil {
edesc = *e.Description
}
errorCollection += fmt.Sprintf("%s: %s\n", etype, edesc)
}
}
return s, false, fmt.Errorf("received status %s from server\n%s", status, errorCollection)
}
maxFailCount--
return s, false, nil
}
if status == instances.STATUS_TERMINATING {
return s, false, errors.New("received status TERMINATING from server")
}
return s, false, nil
})
}

// Wait will wait for instance deletion
// returned value for deletion wait will always be nil
func (DeleteResponse) WaitHandler(ctx context.Context, c *instances.ClientWithResponses, projectID, name string) *wait.Handler {
return wait.New(func() (interface{}, bool, error) {
res, err := c.Get(ctx, projectID, name)
if err = validate.Response(res, err); err != nil {
if res != nil && res.StatusCode() == http.StatusNotFound {
return nil, true, nil
}
return nil, false, err
}
return nil, false, nil
})
}
14 changes: 14 additions & 0 deletions internal/config/load-balancer/1.3.0/include/project/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// this file is only used to prevent wait.go
// from showing errors

package project

import "github.com/SchwarzIT/community-stackit-go-client/pkg/services/load-balancer/1beta.0.0/project"

type EnableProjectResponse struct {
project.ClientWithResponsesInterface
}

type DisableProjectResponse struct {
project.ClientWithResponsesInterface
}
53 changes: 53 additions & 0 deletions internal/config/load-balancer/1.3.0/include/project/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package project

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/SchwarzIT/community-stackit-go-client/pkg/services/load-balancer/1beta.0.0/project"
"github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
)

func (*EnableProjectResponse) WaitHandler(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.GetStatus(ctx, projectID)
if err = validate.Response(resp, err, "JSON200.Status"); err != nil {
return nil, false, err
}
switch *resp.JSON200.Status {
case project.STATUS_FAILED:
fallthrough
case project.STATUS_DELETING:
return nil, false, fmt.Errorf("received state: %s for project ID: %s",
*resp.JSON200.Status,
projectID,
)
case project.STATUS_UNSPECIFIED:
// in some cases beta APIs do not return a status
fallthrough
case project.STATUS_READY:
return nil, true, nil
}
return nil, false, nil
})
}

func (*DisableProjectResponse) WaitHandler(ctx context.Context, c *project.ClientWithResponses, projectID string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.GetStatus(ctx, projectID)
if err = validate.Response(resp, err, "JSON200.Status"); err != nil {
if strings.Contains(err.Error(), http.StatusText(http.StatusNotFound)) {
return nil, true, nil
}
return nil, false, err
}
if *resp.JSON200.Status == project.STATUS_DISABLED ||
*resp.JSON200.Status == project.STATUS_UNSPECIFIED {
return nil, true, nil
}
return nil, false, nil
})
}
20 changes: 20 additions & 0 deletions internal/config/load-balancer/1.3.0/include/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package loadbalancer

import (
"github.com/SchwarzIT/community-stackit-go-client/pkg/baseurl"
"github.com/SchwarzIT/community-stackit-go-client/pkg/contracts"
loadbalancer "github.com/SchwarzIT/community-stackit-go-client/pkg/services/load-balancer/1beta.0.0"
)

var BaseURLs = baseurl.New(
"load_balancer",
"https://load-balancer.api.eu01.stackit.cloud",
)

func NewService(c contracts.BaseClientInterface) *loadbalancer.ClientWithResponses {
nc, _ := loadbalancer.NewClient(
BaseURLs.Get(),
loadbalancer.WithHTTPClient(c),
)
return nc
}
Loading

0 comments on commit 0f3ca1f

Please sign in to comment.