Newer
Older
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2017 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//+build ignore
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"log"
"os"
"text/template"
"time"
)
func main() {
typeName := flag.String("type-name", "Example", "Name of the type to use (e.g. TenantGroup).")
serviceName := flag.String("service-name", "ExampleService", "Name of the service to create (e.g. TenantGroupsService).")
endpoint := flag.String("endpoint", "tenancy", "Name of the endpoint (e.g. dcim, ipam, tenancy).")
service := flag.String("service", "example", "Name of the service below endpoint (e.g. tenant-groups).")
updateTypeName := flag.String("update-type-name", "", "Name of the type to use for creates and updates, to change the marshal behavior. Default typeName.")
withoutListOpts := flag.Bool("without-list-opts", false, "Disable list options for this endpoint.")
flag.Parse()
if *updateTypeName == "" {
*updateTypeName = *typeName
}
b := &bytes.Buffer{}
functionsTemplate.Execute(b, struct {
Timestamp time.Time
TypeName string
UpdateTypeName string
ServiceName string
Endpoint string
Service string
ListOpts bool
}{
Timestamp: time.Now(),
TypeName: *typeName,
UpdateTypeName: *updateTypeName,
ServiceName: *serviceName,
Endpoint: *endpoint,
Service: *service,
ListOpts: !*withoutListOpts,
})
// go fmt
res, err := format.Source(b.Bytes())
if err != nil {
log.Fatal(err)
}
f, err := os.Create(fmt.Sprintf("%s_%s.go", *endpoint, *service))
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.Write(res)
if err != nil {
log.Fatal(err)
}
}
var functionsTemplate = template.Must(template.New("").Parse(`// Copyright 2017 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by generate_functions.go. DO NOT EDIT.
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package netbox
import (
"encoding/json"
"fmt"
"net/http"
)
// {{ .ServiceName }} is used in a Client to access NetBox's {{ .Endpoint }}/{{ .Service }} API methods.
type {{ .ServiceName }} struct {
c *Client
}
// Get retrieves an {{ .TypeName }} object from NetBox by its ID.
func (s *{{ .ServiceName }}) Get(id int) (*{{ .TypeName }}, error) {
req, err := s.c.NewRequest(
http.MethodGet,
fmt.Sprintf("api/{{ .Endpoint }}/{{ .Service }}/%d/", id),
nil,
)
if err != nil {
return nil, err
}
t := new({{ .TypeName }})
err = s.c.Do(req, t)
if err != nil {
return nil, err
}
return t, nil
}
// List returns a Page associated with an NetBox API Endpoint.
{{ if .ListOpts -}}
func (s *{{ .ServiceName }}) List(options *List{{ .TypeName }}Options) *Page {
return NewPage(s.c, "api/{{ .Endpoint }}/{{ .Service }}/", options)
}
{{ else -}}
func (s *{{ .ServiceName }}) List() *Page {
return NewPage(s.c, "api/{{ .Endpoint }}/{{ .Service }}/", nil)
}
{{ end }}
// Extract retrives a list of {{ .TypeName }} objects from page.
func (s *{{ .ServiceName }}) Extract(page *Page) ([]*{{ .TypeName }}, error) {
if err := page.Err(); err != nil {
return nil, err
}
var groups []*{{ .TypeName }}
if err := json.Unmarshal(page.data.Results, &groups); err != nil {
return nil, err
}
return groups, nil
}
// Create creates a new {{ .TypeName }} object in NetBox and returns the ID of the new object.
func (s *{{ .ServiceName }}) Create(data *{{ .TypeName }}) (int, error) {
req, err := s.c.NewJSONRequest(http.MethodPost, "api/{{ .Endpoint }}/{{ .Service }}/", nil, data)
if err != nil {
}
g := new({{ .UpdateTypeName }})
err = s.c.Do(req, g)
if err != nil {
// Update changes an existing {{ .TypeName }} object in NetBox, and returns the ID of the new object.
func (s *{{ .ServiceName }}) Update(data *{{ .TypeName }}) (int, error) {
req, err := s.c.NewJSONRequest(
http.MethodPatch,
fmt.Sprintf("api/{{ .Endpoint }}/{{ .Service }}/%d/", data.ID),
nil,
if err != nil {
}
// g is just used to verify correct api result.
// data is not changed, because the g is not the full representation that one would
// get with Get. But if the response was unmarshaled into {{ .UpdateTypeName }} correctly,
// everything went fine, and we do not need to update data.
g := new({{ .UpdateTypeName }})
err = s.c.Do(req, g)
if err != nil {
}
// Delete deletes an existing {{ .TypeName }} object from NetBox.
func (s *{{ .ServiceName }}) Delete(data *{{ .TypeName }}) error {
req, err := s.c.NewRequest(
http.MethodDelete,
fmt.Sprintf("api/{{ .Endpoint }}/{{ .Service }}/%d/", data.ID),
if err != nil {
return err
}
return s.c.Do(req, nil)
}
`))