Added static DNS record

This commit is contained in:
appkins
2024-07-13 04:52:27 -05:00
parent 85af09f5e0
commit e99c608dec
62 changed files with 1406 additions and 11 deletions

View File

@@ -64,6 +64,8 @@ func (dst *{{ $k }}) UnmarshalJSON(b []byte) error {
}
{{ end }}
{{- $apiVersion2 := or (eq .StructName "APGroup") (eq .StructName "DNSRecord") }}
{{ if not .IsSetting }}
func (c *Client) list{{ .StructName }}(ctx context.Context, site string) ([]{{ .StructName }}, error) {
var respBody struct {
@@ -71,7 +73,7 @@ func (c *Client) list{{ .StructName }}(ctx context.Context, site string) ([]{{ .
Data []{{ .StructName }} `json:"data"`
}
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/{{ if eq .StructName "Device" }}stat/{{else if eq .StructName "APGroup" }}{{ else }}rest/{{ end }}{{ .ResourcePath }}", site), nil, &respBody)
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/{{ if eq .StructName "Device" }}stat/{{ else if $apiVersion2 }}{{ else }}rest/{{ end }}{{ .ResourcePath }}", site), nil, &respBody)
if err != nil {
return nil, err
}
@@ -88,7 +90,7 @@ func (c *Client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSett
{{ if .IsSetting }}
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/get/setting/{{ .ResourcePath }}", site), nil, &respBody)
{{- else }}
err := c.do(ctx, "GET", fmt.Sprintf("s/%s/{{ if eq .StructName "Device" }}stat{{ else }}rest{{ end }}/{{ .ResourcePath }}/%s", site, id), nil, &respBody)
err := c.do(ctx, "GET", fmt.Sprintf("{{ if eq .StructName "DNSRecord" }}%s/site/%s/{{ .ResourcePath }}/%s", c.apiV2Path{{ else }}s/%s/{{ if eq .StructName "Device" }}stat/{{ else }}rest/{{ end }}{{ .ResourcePath }}/%s"{{ end }}, site, id), nil, &respBody)
{{- end }}
if err != nil {
return nil, err
@@ -104,7 +106,7 @@ func (c *Client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSett
{{ if not .IsSetting }}
func (c *Client) delete{{ .StructName }}(ctx context.Context, site, id string) error {
err := c.do(ctx, "DELETE", fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}/%s", site, id), struct{}{}, nil)
err := c.do(ctx, "DELETE", fmt.Sprintf("{{ if eq .StructName "DNSRecord" }}%s/site/%s/{{ .ResourcePath }}/%s", c.apiV2Path{{ else }}s/%s/rest/{{ .ResourcePath }}/%s"{{ end }}, site, id), struct{}{}, nil)
if err != nil {
return err
}
@@ -117,7 +119,7 @@ func (c *Client) create{{ .StructName }}(ctx context.Context, site string, d *{{
Data []{{ .StructName }} `json:"data"`
}
err := c.do(ctx, "POST", fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}", site), d, &respBody)
err := c.do(ctx, "POST", fmt.Sprintf("{{ if eq .StructName "DNSRecord" }}%s/site/%s/{{ .ResourcePath }}", c.apiV2Path{{ else }}s/%s/rest/{{ .ResourcePath }}"{{ end }}, site), d, &respBody)
if err != nil {
return nil, err
}
@@ -141,7 +143,7 @@ func (c *Client) update{{ .StructName }}(ctx context.Context, site string, d *{{
d.Key = "{{ .ResourcePath }}"
err := c.do(ctx, "PUT", fmt.Sprintf("s/%s/set/setting/{{ .ResourcePath }}", site), d, &respBody)
{{- else }}
err := c.do(ctx, "PUT", fmt.Sprintf("s/%s/rest/{{ .ResourcePath }}/%s", site, d.ID), d, &respBody)
err := c.do(ctx, "PUT", fmt.Sprintf("{{ if eq .StructName "DNSRecord" }}%s/site/%s/{{ .ResourcePath }}/%s", c.apiV2Path{{ else }}s/%s/rest/{{ .ResourcePath }}/%s"{{ end }}, site, d.ID), d, &respBody)
{{- end }}
if err != nil {
return nil, err

51
fields/client.go.tmpl Normal file
View File

@@ -0,0 +1,51 @@
{{- $structName := .StructName }}
{{ define "field" }}
{{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .FieldType }} `json:"{{ .JSONName }}{{ if .OmitEmpty }},omitempty{{ end }}"` {{ if .FieldValidation }}// {{ .FieldValidation }}{{ end }} {{- end }}
{{ define "field-customUnmarshalType" }}
{{- if eq .CustomUnmarshalType "" }}{{else}}
{{ .FieldName }} {{ if .IsArray }}[]{{end}}{{ .CustomUnmarshalType }} `json:"{{ .JSONName }}"`{{ end }} {{- end }}
{{ define "typecast" }}
{{- if ne .CustomUnmarshalFunc "" }}
dst.{{ .FieldName }}= {{ .CustomUnmarshalFunc }}(aux.{{ .FieldName }})
{{- else if eq .CustomUnmarshalType "" }}{{else}}
{{- if .IsArray }}
dst.{{ .FieldName }}= make([]{{ .FieldType }}, len(aux.{{ .FieldName }}))
for i, v := range aux.{{ .FieldName }} {
dst.{{ .FieldName }}[i] = {{ .FieldType }}(v)
}
{{- else }}
dst.{{ .FieldName }} = {{ .FieldType }}(aux.{{ .FieldName }})
{{- end }}{{- end }}{{- end }}
// Code generated from ace.jar fields *.json files
// DO NOT EDIT.
package unifi
import (
"context"
)
{{ if not .IsSetting }}
func (c *Client) List{{ .StructName }}(ctx context.Context, site string) ([]{{ .StructName }}, error) {
return c.list{{ .StructName }}(ctx, site)
}
{{- end }}
func (c *Client) Get{{ .StructName }}(ctx context.Context, site{{ if not .IsSetting }}, id{{ end }} string) (*{{ .StructName }}, error) {
return c.get{{ .StructName }}(ctx, site{{ if not .IsSetting }}, id{{ end }})
}
{{ if not .IsSetting }}
func (c *Client) Delete{{ .StructName }}(ctx context.Context, site, id string) error {
return c.delete{{ .StructName }}(ctx, site, id)
}
func (c *Client) Create{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
return c.create{{ .StructName }}(ctx, site, d)
}
{{- end }}
func (c *Client) Update{{ .StructName }}(ctx context.Context, site string, d *{{ .StructName }}) (*{{ .StructName }}, error) {
return c.update{{ .StructName }}(ctx, site, d)
}

View File

@@ -0,0 +1,10 @@
{
"enabled": "false|true",
"key": ".{1,128}",
"port": "[0-9]{1,8}",
"priority": ".{1,128}",
"record_type": "A|AAAA|CNAME|MX|NS|PTR|SOA|SRV|TXT",
"ttl": "[0-9]{1,10}",
"value": ".{1,256}",
"weight": "[0-9]{1,10}"
}

View File

@@ -159,6 +159,63 @@ func extractJSON(jarFile, fieldsDir string) error {
}
}
wd, err := os.Getwd()
if err != nil {
panic(err)
}
rootDir := findModuleRoot(wd)
srcDir := path.Join(rootDir, "fields")
files, err := os.ReadDir(path.Join(srcDir, "custom"))
if err != nil {
return fmt.Errorf("unable to read custom directory: %w", err)
}
for _, file := range files {
if !file.IsDir() {
fs, err := os.Open(path.Join(srcDir, "custom", file.Name()))
if err != nil {
return fmt.Errorf("unable to open file: %w", err)
}
defer fs.Close()
rf, err := os.Create(filepath.Join(fieldsDir, file.Name()))
if err != nil {
return fmt.Errorf("unable to create file: %w", err)
}
defer rf.Close()
_, err = io.Copy(rf, fs)
if err != nil {
return fmt.Errorf("unable to copy file: %w", err)
}
_, err = io.ReadAll(fs)
if err != nil {
return fmt.Errorf("unable to read file: %w", err)
}
}
fmt.Println(file.Name(), file.IsDir())
}
// TODO: cleanup JSON
return nil
}
func findModuleRoot(dir string) (roots string) {
if dir == "" {
panic("dir not set")
}
dir = filepath.Clean(dir)
// Look for enclosing go.mod.
for {
if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
return dir
}
d := filepath.Dir(dir)
if d == dir {
break
}
dir = d
}
return ""
}

View File

@@ -148,6 +148,8 @@ func NewResource(structName string, resourcePath string) *Resource {
// Removed in v7, retaining for backwards compatibility
baseType.Fields["MdnsEnabled"] = NewFieldInfo("MdnsEnabled", "mdns_enabled", "bool", "", false, false, "")
}
case resource.StructName == "DNSRecord":
resource.ResourcePath = "static-dns"
case resource.StructName == "Device":
baseType.Fields[" MAC"] = NewFieldInfo("MAC", "mac", "string", "", true, false, "")
baseType.Fields["Adopted"] = NewFieldInfo("Adopted", "adopted", "bool", "", false, false, "")
@@ -429,7 +431,7 @@ func main() {
}
var code string
if code, err = resource.generateCode(); err != nil {
if code, err = resource.generateCode(false); err != nil {
panic(err)
}
@@ -437,6 +439,22 @@ func main() {
if err := os.WriteFile(filepath.Join(outDir, goFile), ([]byte)(code), 0o644); err != nil {
panic(err)
}
implFile := strcase.ToSnake(structName) + ".go"
implFilePath := filepath.Join(outDir, implFile)
if _, err := os.Stat(implFilePath); err != nil {
if errors.Is(err, os.ErrNotExist) {
var implCode string
if implCode, err = resource.generateCode(true); err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(implFilePath), ([]byte)(implCode), 0o644); err != nil {
panic(err)
}
}
}
}
// Write version file.
@@ -583,12 +601,20 @@ func (r *Resource) processJSON(b []byte) error {
//go:embed api.go.tmpl
var apiGoTemplate string
func (r *Resource) generateCode() (string, error) {
//go:embed client.go.tmpl
var clientGoTemplate string
func (r *Resource) generateCode(isImpl bool) (string, error) {
var err error
var buf bytes.Buffer
writer := io.Writer(&buf)
tpl := template.Must(template.New("api.go.tmpl").Parse(apiGoTemplate))
var tpl *template.Template
if isImpl {
tpl = template.Must(template.New("client.go.tmpl").Parse(clientGoTemplate))
} else {
tpl = template.Must(template.New("api.go.tmpl").Parse(apiGoTemplate))
}
err = tpl.Execute(writer, r)
if err != nil {