Skip to content

Commit

Permalink
add additional APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Sep 24, 2022
1 parent 2b007d1 commit 56d84fd
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 75 deletions.
18 changes: 10 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
PWD = $(shell pwd)
UID = $(shell id -u)
GID = $(shell id -g)
VERSION = "0.1.7"
GO_FLAGS = -ldflags "-X beryju.io/gravity/pkg/extconfig.Version=$(VERSION)" -v
VERSION = "0.1.8"
GO_FLAGS = -ldflags "-X beryju.io/gravity/pkg/extconfig.Version=${VERSION}" -v
SCHEMA_FILE = schema.yml

docker-build:
go build \
${GO_FLAGS} \
-ldflags "-X beryju.io/gravity/pkg/extconfig.BuildHash=$(GIT_BUILD_HASH)" \
-ldflags "-X beryju.io/gravity/pkg/extconfig.BuildHash=${GIT_BUILD_HASH}" \
-a -o gravity .

run:
INSTANCE_LISTEN=0.0.0.0 DEBUG=true LISTEN_ONLY=true go run $(GO_FLAGS) . server
INSTANCE_LISTEN=0.0.0.0 DEBUG=true LISTEN_ONLY=true go run ${GO_FLAGS} . server

gen-build:
DEBUG=true go run $(GO_FLAGS) . cli generateSchema schema.yml
DEBUG=true go run ${GO_FLAGS} . cli generateSchema ${SCHEMA_FILE}
git add ${SCHEMA_FILE}

gen-clean:
rm -rf gen-ts-api/
Expand All @@ -25,17 +27,17 @@ gen-client-ts:
--rm -v ${PWD}:/local \
--user ${UID}:${GID} \
openapitools/openapi-generator-cli:v6.0.0 generate \
-i /local/schema.yml \
-i /local/${SCHEMA_FILE} \
-g typescript-fetch \
-o /local/gen-ts-api \
--additional-properties=typescriptThreePlus=true,supportsES6=true,npmName=gravity-api,npmVersion=$(VERSION) \
--additional-properties=typescriptThreePlus=true,supportsES6=true,npmName=gravity-api,npmVersion=${VERSION} \
--git-repo-id BeryJu \
--git-user-id gravity
cd gen-ts-api && npm i

gen-client-ts-update: gen-client-ts
cd gen-ts-api && npm publish
cd web && npm i gravity-api@$(VERSION)
cd web && npm i gravity-api@${VERSION}
cd web && git add package*.json

gen: gen-build gen-clean gen-client-ts-update
Expand Down
6 changes: 3 additions & 3 deletions pkg/extconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type ExtConfig struct {
}

type ExtConfigDirs struct {
EtcdDir string
CertDir string
BackupDir string
EtcdDir string `json:"etcdDir"`
CertDir string `json:"certDir"`
BackupDir string `json:"backupDir"`
}

var globalExtConfig *ExtConfig
Expand Down
86 changes: 86 additions & 0 deletions pkg/instance/api_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package instance

import (
"context"
"encoding/json"
"strings"

"beryju.io/gravity/pkg/extconfig"
"beryju.io/gravity/pkg/instance/types"
"beryju.io/gravity/pkg/roles"
apitypes "beryju.io/gravity/pkg/roles/api/types"
"github.com/swaggest/rest/web"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
clientv3 "go.etcd.io/etcd/client/v3"
)

func (i *Instance) setupInstanceAPI() {
i.ForRole("instance").AddEventListener(apitypes.EventTopicAPIMuxSetup, func(ev *roles.Event) {
svc := ev.Payload.Data["svc"].(*web.Service)
svc.Get("/api/v1/instances", i.apiHandlerInstances())
svc.Get("/api/v1/info", i.apiHandlerInfo())
})
}

func (i *Instance) apiHandlerInstances() usecase.Interactor {
type instancesOutput struct {
Instances []InstanceInfo `json:"instances" required:"true"`
}
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *instancesOutput) error {
prefix := i.kv.Key(types.KeyInstance).Prefix(true).String()
instances, err := i.kv.Get(
ctx,
prefix,
clientv3.WithPrefix(),
)
if err != nil {
return status.Wrap(err, status.Internal)
}
for _, ri := range instances.Kvs {
// We only want one level
relKey := strings.TrimPrefix(string(ri.Key), prefix)
if strings.Contains(relKey, "/") {
continue
}
var inst InstanceInfo
err := json.Unmarshal(ri.Value, &inst)
if err != nil {
i.log.WithError(err).Warning("failed to parse instance info")
continue
}
output.Instances = append(output.Instances, inst)
}
return nil
})
u.SetName("root.get_instances")
u.SetTitle("Instances")
u.SetTags("instances")
u.SetExpectedErrors(status.Internal)
return u
}

func (i *Instance) apiHandlerInfo() usecase.Interactor {
type systemInfo struct {
Version string `json:"version" required:"true"`
BuildHash string `json:"buildHash" required:"true"`

Dirs *extconfig.ExtConfigDirs `json:"dirs" required:"true"`

CurrentInstanceIdentifier string `json:"currentInstanceIdentifier" required:"true"`
CurrentInstanceIP string `json:"currentInstanceIP" required:"true"`
}
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *systemInfo) error {
output.Version = extconfig.Version
output.BuildHash = extconfig.BuildHash
output.Dirs = extconfig.Get().Dirs()
output.CurrentInstanceIP = extconfig.Get().Instance.IP
output.CurrentInstanceIdentifier = extconfig.Get().Instance.Identifier
return nil
})
u.SetName("root.get_info")
u.SetTitle("Instances")
u.SetTags("instances")
u.SetExpectedErrors(status.Internal)
return u
}
48 changes: 0 additions & 48 deletions pkg/instance/info.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package instance

import (
"context"
"encoding/json"
"strings"

"beryju.io/gravity/pkg/extconfig"
"beryju.io/gravity/pkg/instance/types"
"beryju.io/gravity/pkg/roles"
apitypes "beryju.io/gravity/pkg/roles/api/types"
"github.com/swaggest/rest/web"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
clientv3 "go.etcd.io/etcd/client/v3"
)

Expand All @@ -31,48 +24,7 @@ func (i *Instance) getInfo() *InstanceInfo {
}
}

func (i *Instance) apiHandlerInstances() usecase.Interactor {
type instancesOutput struct {
Instances []InstanceInfo `json:"instances" required:"true"`
}
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *instancesOutput) error {
prefix := i.kv.Key(types.KeyInstance).Prefix(true).String()
instances, err := i.kv.Get(
ctx,
prefix,
clientv3.WithPrefix(),
)
if err != nil {
return status.Wrap(err, status.Internal)
}
for _, ri := range instances.Kvs {
// We only want one level
relKey := strings.TrimPrefix(string(ri.Key), prefix)
if strings.Contains(relKey, "/") {
continue
}
var inst InstanceInfo
err := json.Unmarshal(ri.Value, &inst)
if err != nil {
i.log.WithError(err).Warning("failed to parse instance info")
continue
}
output.Instances = append(output.Instances, inst)
}
return nil
})
u.SetName("root.get_instances")
u.SetTitle("Instances")
u.SetTags("instances")
u.SetExpectedErrors(status.Internal)
return u
}

func (i *Instance) writeInstanceInfo() {
i.ForRole("instance_info").AddEventListener(apitypes.EventTopicAPIMuxSetup, func(ev *roles.Event) {
svc := ev.Payload.Data["svc"].(*web.Service)
svc.Get("/api/v1/instances", i.apiHandlerInstances())
})
ji, err := json.Marshal(i.getInfo())
if err != nil {
i.log.WithError(err).Warning("failed to get instance info")
Expand Down
1 change: 1 addition & 0 deletions pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (i *Instance) getRoles() []string {
func (i *Instance) bootstrap() {
i.log.Trace("bootstrapping instance")
i.writeInstanceInfo()
i.setupInstanceAPI()
for _, roleId := range i.getRoles() {
instanceRoles.WithLabelValues(roleId).Add(1)
ctx, cancel := context.WithCancel(i.rootContext)
Expand Down
77 changes: 76 additions & 1 deletion schema.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.3
info:
title: gravity
version: 0.1.7
version: 0.1.8
paths:
/api/v1/auth/config:
get:
Expand Down Expand Up @@ -489,6 +489,33 @@ paths:
summary: Discovery Subnets
tags:
- roles/discovery
/api/v1/discovery/subnets/start:
post:
operationId: discovery.subnet_start
parameters:
- in: query
name: identifier
required: true
schema:
type: string
responses:
"204":
description: No Content
"400":
content:
application/json:
schema:
$ref: '#/components/schemas/RestErrResponse'
description: Bad Request
"500":
content:
application/json:
schema:
$ref: '#/components/schemas/RestErrResponse'
description: Internal Server Error
summary: Discovery Subnets
tags:
- roles/discovery
/api/v1/dns/zones:
delete:
operationId: dns.delete_zones
Expand Down Expand Up @@ -722,6 +749,25 @@ paths:
summary: Etcd members
tags:
- roles/etcd
/api/v1/info:
get:
operationId: root.get_info
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/InstanceSystemInfo'
description: OK
"500":
content:
application/json:
schema:
$ref: '#/components/schemas/RestErrResponse'
description: Internal Server Error
summary: Instances
tags:
- instances
/api/v1/instances:
get:
operationId: root.get_instances
Expand Down Expand Up @@ -1484,6 +1530,16 @@ components:
nullable: true
type: array
type: object
ExtconfigExtConfigDirs:
nullable: true
properties:
backupDir:
type: string
certDir:
type: string
etcdDir:
type: string
type: object
InstanceInstanceInfo:
properties:
identifier:
Expand All @@ -1510,6 +1566,25 @@ components:
required:
- instances
type: object
InstanceSystemInfo:
properties:
buildHash:
type: string
currentInstanceIP:
type: string
currentInstanceIdentifier:
type: string
dirs:
$ref: '#/components/schemas/ExtconfigExtConfigDirs'
version:
type: string
required:
- version
- buildHash
- dirs
- currentInstanceIdentifier
- currentInstanceIP
type: object
MonitoringRoleConfig:
nullable: true
properties:
Expand Down
14 changes: 7 additions & 7 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"eslint-config-google": "^0.14.0",
"eslint-plugin-custom-elements": "0.0.6",
"eslint-plugin-lit": "^1.6.1",
"gravity-api": "^0.1.7",
"gravity-api": "^0.1.8",
"lit": "^2.3.1",
"prettier": "^2.7.1",
"rollup": "^2.79.1",
Expand Down
Loading

0 comments on commit 56d84fd

Please sign in to comment.