-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_instance.go
53 lines (45 loc) · 1.37 KB
/
create_instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package endpoint
import (
"context"
"fmt"
instancev1 "github.com/cownetwork/instance-controller/api/v1"
"github.com/cownetwork/instance-service/kubernetes"
"github.com/cownetwork/instance-service/template"
"github.com/go-kit/kit/endpoint"
)
type CreateInstanceRequest struct {
TemplateName string
TemplateInstanceName string
Instance *instancev1.Instance
}
type CreateInstanceResponse struct {
Instance *instancev1.Instance
}
func MakeCreateInstanceEndpoint(kubesvc kubernetes.Service, templatesvc template.Service) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
const op = "endpoint/MakeCreateInstanceEndpoint"
create := req.(CreateInstanceRequest)
var instance *instancev1.Instance
if create.Instance != nil {
created, err := kubesvc.CreateInstance(ctx, create.Instance)
if err != nil {
return nil, fmt.Errorf("%s: %v", op, err)
}
instance = created
} else if create.TemplateName != "" {
templated, ok := templatesvc.Get(create.TemplateName)
if !ok {
return nil, fmt.Errorf("%s: no template found", op)
}
templated.Name = create.TemplateInstanceName
created, err := kubesvc.CreateInstance(ctx, templated)
if err != nil {
return nil, fmt.Errorf("%s: %v", op, err)
}
instance = created
}
return CreateInstanceResponse{
Instance: instance,
}, nil
}
}