Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate plugins and load openapi from sc during client generation #1705

Open
wants to merge 2 commits into
base: sc-poc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package driver

import "github.com/getkin/kin-openapi/openapi3"
import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/spacecloud-io/space-cloud/pkg/apis/core/v1alpha1"
)

type Driver interface {
// GenerateTypes takes openapi spec and generates types in the respective languages
GenerateTypes(*openapi3.T) (string, string, error)

// GenerateAPI takes openapi spec and generates apis in the respective languages
GenerateAPIs(*openapi3.T) (string, string, error)

// GeneratePlugins takes list of plugins provided by SpaceCloud and generates plugins
// in the respective languages
GeneratePlugins([]v1alpha1.HTTPPlugin) (string, string, error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package golang

import (
"fmt"
"strings"

"github.com/spacecloud-io/space-cloud/pkg/apis/core/v1alpha1"
"golang.org/x/tools/imports"
)

func (goDriver *Golang) GeneratePlugins(plugins []v1alpha1.HTTPPlugin) (string, string, error) {
fileName := "plugins.gen.go"
var b strings.Builder

// package name and imports
pkgOut := fmt.Sprintf("package %s\n\n", goDriver.pkgName)
_, _ = b.WriteString(pkgOut)

s := `
type Plugins struct {}

type PluginDetails struct {
name string
driver string
}

func (plugin PluginDetails) Name() string {
return plugin.name
}

func (plugin PluginDetails) Driver() string {
return plugin.driver
}

`

for _, plugin := range plugins {
driverName := getTypeName(plugin.Driver, false) + getTypeName(plugin.Name, false)
s += fmt.Sprintf("func (plugin Plugins) %s() PluginDetails {\n", driverName)
s += fmt.Sprintf("%s := PluginDetails{\n", driverName)
s += fmt.Sprintf("name: %q,\n", plugin.Name)
s += fmt.Sprintf("driver: %q,\n}\n", plugin.Driver)
s += fmt.Sprintf("return %s\n}\n\n", driverName)
}
_, _ = b.WriteString(s)

outBytes, err := imports.Process(goDriver.pkgName+".go", []byte(b.String()), nil)
if err != nil {
return "", "", fmt.Errorf("error formatting Go code: %w", err)
}
return string(outBytes), fileName, nil
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package rtk

import "github.com/spacecloud-io/space-cloud/cmd/spacectl/commands/client/generate/driver"
import (
"github.com/spacecloud-io/space-cloud/cmd/spacectl/commands/client/generate/driver"
"github.com/spacecloud-io/space-cloud/pkg/apis/core/v1alpha1"
)

type RTK struct {
name string
Expand All @@ -13,4 +16,8 @@ func MakeRTKDriver(name string) driver.Driver {
return rtkDriver
}

func (r *RTK) GeneratePlugins([]v1alpha1.HTTPPlugin) (string, string, error) {
return "", "", nil
}

var _ driver.Driver = (*RTK)(nil)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ func getTypeName(name string, skipFirst bool) string {

arr[i] = strings.Title(item)
}
s1 := strings.Join(arr, "")

arr = strings.Split(s1, "_")
for i, item := range arr {
if i == 0 && skipFirst {
arr[i] = item
continue
}

arr[i] = strings.Title(item)
}

return strings.Join(arr, "")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package typescript

import (
"fmt"
"strings"

"github.com/spacecloud-io/space-cloud/pkg/apis/core/v1alpha1"
)

func (t *Typescript) GeneratePlugins(plugins []v1alpha1.HTTPPlugin) (string, string, error) {
fileName := "plugins.ts"
var b strings.Builder

s :=
`interface PluginDetails {
name: string,
driver: string
}

export class Plugins {
`

for _, plugin := range plugins {
driverName := getTypeName(plugin.Driver, false) + getTypeName(plugin.Name, false)
s += fmt.Sprintf(" %s = (): PluginDetails => {\n", driverName)
s += fmt.Sprintf(" const %s: PluginDetails = {\n", driverName)
s += fmt.Sprintf(" name: %q,\n", plugin.Name)
s += fmt.Sprintf(" driver: %q,\n", plugin.Driver)
s += " }\n"
s += fmt.Sprintf(" return %s\n", driverName)
s += " }\n\n"
}

s += "}\n"
_, _ = b.WriteString(s)
return b.String(), fileName, nil
}
65 changes: 53 additions & 12 deletions sc-poc/cmd/spacectl/commands/client/generate/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package generate

import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
Expand All @@ -11,6 +13,8 @@ import (
"github.com/spacecloud-io/space-cloud/cmd/spacectl/commands/client/generate/driver/golang"
"github.com/spacecloud-io/space-cloud/cmd/spacectl/commands/client/generate/driver/rtk"
"github.com/spacecloud-io/space-cloud/cmd/spacectl/commands/client/generate/driver/typescript"
"github.com/spacecloud-io/space-cloud/utils"
clientutils "github.com/spacecloud-io/space-cloud/utils/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -29,18 +33,33 @@ func NewCommand() *cobra.Command {
_ = viper.BindPFlag("name", cmd.Flags().Lookup("name"))
_ = viper.BindPFlag("lang", cmd.Flags().Lookup("lang"))
_ = viper.BindPFlag("package", cmd.Flags().Lookup("package"))
_ = viper.BindPFlag("sc-url", cmd.Flags().Lookup("sc-url"))
},
RunE: func(cmd *cobra.Command, args []string) error {
config := viper.GetString("config")
output := viper.GetString("output")
name := viper.GetString("name")
lang := viper.GetString("lang")
pkgName := viper.GetString("package")
scUrl := viper.GetString("sc-url")

doc, err := openapi3.NewLoader().LoadFromFile(config)
if err != nil {
fmt.Println("Unable to openapi doc:", err)
os.Exit(1)
var doc *openapi3.T
if scUrl != "" {
urlStr := utils.EnsureTrailingSlash(scUrl) + "v1/api/openapi.json"
configUrl, err := url.Parse(urlStr)
if err != nil {
log.Fatal("Error parsing URL:", err)
}
doc, err = openapi3.NewLoader().LoadFromURI(configUrl)
if err != nil {
log.Fatal("Unable to openapi doc:", err)
}
} else {
fileDoc, err := openapi3.NewLoader().LoadFromFile(config)
if err != nil {
log.Fatal("Unable to openapi doc:", err)
}
doc = fileDoc
}

var driver driver.Driver
Expand All @@ -52,33 +71,54 @@ func NewCommand() *cobra.Command {
_ = os.MkdirAll(output, 0777)
_ = os.WriteFile(filepath.Join(output, "helpers.ts"), []byte(rtk.HelperTS), 0777)
_ = os.WriteFile(filepath.Join(output, "index.ts"), []byte(rtk.IndexTS), 0777)
if _, err = os.Stat(filepath.Join(output, "http.config.ts")); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(output, "http.config.ts")); os.IsNotExist(err) {
_ = os.WriteFile(filepath.Join(output, "http.config.ts"), []byte(rtk.ConfigTS), 0777)
}
case "go":
driver = golang.MakeGoDriver(pkgName)
case "typescript":
driver = typescript.MakeTSDriver()
default:
fmt.Printf("Invalid language name or language %s not supported.\n", lang)
return nil
log.Fatalf("Invalid language name or language %s not supported.\n", lang)
}

_ = os.MkdirAll(output, 0777)
api, fileName, err := driver.GenerateAPIs(doc)
if err != nil {
fmt.Printf("error generating api: %s\n", err)
os.Exit(1)
log.Fatalf("error generating api: %s\n", err)
}
_ = os.WriteFile(filepath.Join(output, fileName), []byte(api), 0777)

types, fileName, err := driver.GenerateTypes(doc)
if err != nil {
fmt.Printf("error generating types: %s\n", err)
os.Exit(1)
log.Fatalf("error generating types: %s\n", err)
}
_ = os.WriteFile(filepath.Join(output, fileName), []byte(types), 0777)

if scUrl != "" && lang != "rtk" {
creds, err := clientutils.GetCredentials()
if err != nil {
log.Fatal("Failed to get SpaceCloud credentials: ", err)
}

httpClient := &http.Client{}
token, err := clientutils.Login(httpClient, creds)
if err != nil {
log.Fatal("Failed to authenticate with SpaceCloud: ", err)
}

allPlugins, err := clientutils.ListAllPlugins(httpClient, creds.BaseUrl, token)
if err != nil {
log.Fatal("Failed to list all plugins: ", err)
}

plugins, fileName, err := driver.GeneratePlugins(allPlugins)
if err != nil {
log.Fatalf("error generating plugins: %s\n", err)
}
_ = os.WriteFile(filepath.Join(output, fileName), []byte(plugins), 0777)
}

return nil
},
}
Expand All @@ -88,6 +128,7 @@ func NewCommand() *cobra.Command {
cmd.Flags().StringP("name", "n", "my-api", "Name for the API.")
cmd.Flags().StringP("lang", "l", "go", "Language in which to generate code. Supported languages are 'rtk', 'Go'")
cmd.Flags().StringP("package", "p", "openapi", "The name of the package to generate.")
cmd.Flags().StringP("sc-url", "", "http://localhost:4122", "URL where SpaceCloud is running.")

return cmd
}
2 changes: 1 addition & 1 deletion sc-poc/cmd/spacectl/commands/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewCommand() *cobra.Command {
Password: base64.StdEncoding.EncodeToString([]byte(password)),
BaseUrl: baseUrl,
}
err := clientutils.Login(httpClient, creds)
_, err := clientutils.Login(httpClient, creds)
if err != nil {
log.Fatal("Failed to authenticate with SpaceCloud", err)
}
Expand Down
2 changes: 1 addition & 1 deletion sc-poc/cmd/spacectl/commands/pkg/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func newCommandApply() *cobra.Command {
}

// Login to SpaceCloud
if err := clientutils.Login(httpClient, creds); err != nil {
if _, err := clientutils.Login(httpClient, creds); err != nil {
log.Fatal("Failed to authenticate with SpaceCloud: ", err)
}

Expand Down
2 changes: 1 addition & 1 deletion sc-poc/cmd/spacectl/commands/pkg/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newCommandGet() *cobra.Command {
}

// Login to SpaceCloud
if err := clientutils.Login(httpClient, creds); err != nil {
if _, err := clientutils.Login(httpClient, creds); err != nil {
log.Fatal("Failed to authenticate with SpaceCloud: ", err)
}

Expand Down
21 changes: 12 additions & 9 deletions sc-poc/utils/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GetCredentials() (Credentials, error) {
return creds, nil
}

func Login(client *http.Client, creds Credentials) error {
func Login(client *http.Client, creds Credentials) (string, error) {
b, _ := base64.StdEncoding.DecodeString(creds.Username)
creds.Username = string(b)
b, _ = base64.StdEncoding.DecodeString(creds.Password)
Expand All @@ -42,24 +42,27 @@ func Login(client *http.Client, creds Credentials) error {

req, err := http.NewRequest(http.MethodPost, path, bytes.NewBuffer(payload))
if err != nil {
return err
return "", err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
return "", err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf(string(body))
return "", fmt.Errorf(string(body))
}

return nil
var m map[string]string
json.Unmarshal(body, &m)
return m["token"], nil
}

func UpdateSpaceCloudCredsFile(creds Credentials) (string, error) {
Expand Down
25 changes: 25 additions & 0 deletions sc-poc/utils/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"

"github.com/spacecloud-io/space-cloud/pkg/apis/core/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand Down Expand Up @@ -97,3 +98,27 @@ func DeleteResources(client *http.Client, gvr schema.GroupVersionResource, baseU

return nil
}

func ListAllPlugins(client *http.Client, baseUrl string, token string) ([]v1alpha1.HTTPPlugin, error) {
var allPlugins []v1alpha1.HTTPPlugin

path := baseUrl + "/sc/v1/plugins"
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch plugins")
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch plugins")
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to fetch plugins")
}

json.Unmarshal(body, &allPlugins)
return allPlugins, nil
}
7 changes: 7 additions & 0 deletions sc-poc/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ func Pluralize(word string) string {
plural := pluralize.Plural(word)
return strings.ToLower(plural)
}

func EnsureTrailingSlash(url string) string {
if !strings.HasSuffix(url, "/") {
return url + "/"
}
return url
}