2021-04-21 13:09:09 +03:00
|
|
|
package apilimits
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-04-21 13:09:09 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2021-04-21 13:09:09 +03:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MetadataObject struct {
|
|
|
|
MetadataDir string
|
|
|
|
|
|
|
|
logger *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ec *cli.ExecutionContext, baseDir string) *MetadataObject {
|
|
|
|
return &MetadataObject{
|
|
|
|
MetadataDir: baseDir,
|
|
|
|
logger: ec.Logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *MetadataObject) Validate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *MetadataObject) CreateFiles() error {
|
|
|
|
var v interface{}
|
|
|
|
data, err := yaml.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-23 12:49:44 +03:00
|
|
|
err = ioutil.WriteFile(filepath.Join(o.MetadataDir, o.Filename()), data, 0644)
|
2021-04-21 13:09:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (o *MetadataObject) Build(metadata *yaml.MapSlice) metadataobject.ErrParsingMetadataObject {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(o.MetadataDir, o.Filename()))
|
2021-04-21 13:09:09 +03:00
|
|
|
if err != nil {
|
2021-07-23 12:49:44 +03:00
|
|
|
return o.error(err)
|
2021-04-21 13:09:09 +03:00
|
|
|
}
|
|
|
|
item := yaml.MapItem{
|
2021-07-23 12:49:44 +03:00
|
|
|
Key: o.Key(),
|
2021-04-21 13:09:09 +03:00
|
|
|
}
|
|
|
|
var obj yaml.MapSlice
|
|
|
|
err = yaml.Unmarshal(data, &obj)
|
|
|
|
if err != nil {
|
2021-07-23 12:49:44 +03:00
|
|
|
return o.error(err)
|
2021-04-21 13:09:09 +03:00
|
|
|
}
|
|
|
|
if len(obj) > 0 {
|
|
|
|
item.Value = obj
|
|
|
|
*metadata = append(*metadata, item)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (o *MetadataObject) Export(metadata yaml.MapSlice) (map[string][]byte, metadataobject.ErrParsingMetadataObject) {
|
2021-04-21 13:09:09 +03:00
|
|
|
var apiLimits interface{}
|
|
|
|
for _, item := range metadata {
|
|
|
|
k, ok := item.Key.(string)
|
2021-07-23 12:49:44 +03:00
|
|
|
if !ok || k != o.Key() {
|
2021-04-21 13:09:09 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
apiLimits = item.Value
|
|
|
|
}
|
|
|
|
if apiLimits == nil {
|
|
|
|
o.logger.WithFields(logrus.Fields{
|
2021-07-23 12:49:44 +03:00
|
|
|
"object": o.Key(),
|
2021-04-21 13:09:09 +03:00
|
|
|
"reason": "not found in metadata",
|
2021-07-23 12:49:44 +03:00
|
|
|
}).Debugf("skipped building %s", o.Key())
|
2021-04-21 13:09:09 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
data, err := yaml.Marshal(apiLimits)
|
|
|
|
if err != nil {
|
2021-07-23 12:49:44 +03:00
|
|
|
return nil, o.error(err)
|
2021-04-21 13:09:09 +03:00
|
|
|
}
|
|
|
|
return map[string][]byte{
|
2021-07-23 12:49:44 +03:00
|
|
|
filepath.ToSlash(filepath.Join(o.MetadataDir, o.Filename())): data,
|
2021-04-21 13:09:09 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (o *MetadataObject) Key() string {
|
2021-04-21 13:09:09 +03:00
|
|
|
return "api_limits"
|
|
|
|
}
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (o *MetadataObject) Filename() string {
|
|
|
|
return "api_limits.yaml"
|
|
|
|
}
|
2021-12-09 20:25:54 +03:00
|
|
|
|
|
|
|
func (o *MetadataObject) GetFiles() ([]string, metadataobject.ErrParsingMetadataObject) {
|
|
|
|
rootFile := filepath.Join(o.BaseDirectory(), o.Filename())
|
|
|
|
files, err := metadataobject.DefaultGetFiles(rootFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, o.error(err)
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *MetadataObject) WriteDiff(opts metadataobject.WriteDiffOpts) metadataobject.ErrParsingMetadataObject {
|
|
|
|
err := metadataobject.DefaultWriteDiff(metadataobject.DefaultWriteDiffOpts{From: o, WriteDiffOpts: opts})
|
|
|
|
if err != nil {
|
|
|
|
return o.error(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *MetadataObject) BaseDirectory() string {
|
|
|
|
return o.MetadataDir
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (o *MetadataObject) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject {
|
|
|
|
return metadataobject.NewErrParsingMetadataObject(o, err, additionalContext...)
|
2021-06-18 20:38:29 +03:00
|
|
|
}
|