-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsections.go
101 lines (94 loc) · 2.81 KB
/
sections.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package phpipam
import (
"encoding/json"
"errors"
"net/http"
)
// Sections struct from phpipam
type Sections struct {
Code int `json:"code"`
Success bool `json:"success"`
Data []struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
} `json:"data"`
Message string `json:"message"`
}
// Section struct from phpipam
type Section struct {
Code int `json:"code"`
Success bool `json:"success"`
Data struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
} `json:"data"`
Message string `json:"message"`
}
// SectionsSubnets struct from phpipam
type SectionsSubnets struct {
Code int `json:"code"`
Success bool `json:"success"`
Data []struct {
ID string `json:"id"`
Subnet string `json:"subnet"`
Description string `json:"description"`
} `json:"data"`
Message string `json:"message"`
}
// GetSections Client pointer method to get all sections from phpipam, returns
// Sections struct and error
func (c *Client) GetSections() (Sections, error) {
var sectionsData Sections
req, _ := http.NewRequest("GET", c.ServerURL+"/api/"+c.Application+"/sections/", nil)
body, err := c.Do(req)
if err != nil {
return sectionsData, err
}
err = json.Unmarshal([]byte(body), §ionsData)
if err != nil {
return sectionsData, err
}
if sectionsData.Code != 200 {
return sectionsData, errors.New(sectionsData.Message)
}
return sectionsData, nil
}
// GetSection Client pointer method to get information about a specific section
// using sectionID string, returns Section struct and error
func (c *Client) GetSection(sectionID string) (Section, error) {
var sectionData Section
req, _ := http.NewRequest("GET", c.ServerURL+"/api/"+c.Application+"/sections/"+sectionID+"/", nil)
body, err := c.Do(req)
if err != nil {
return sectionData, err
}
err = json.Unmarshal([]byte(body), §ionData)
if err != nil {
return sectionData, err
}
if sectionData.Code != 200 {
return sectionData, errors.New(sectionData.Message)
}
return sectionData, nil
}
// GetSectionsSubnets Client pointer method to get all subnets within a specific
// phpipam section using sectionID string, returns SectionsSubnets struct and
// error
func (c *Client) GetSectionsSubnets(sectionID string) (SectionsSubnets, error) {
var sectionsSubnetsData SectionsSubnets
req, _ := http.NewRequest("GET", c.ServerURL+"/api/"+c.Application+"/sections/"+sectionID+"/subnets/", nil)
body, err := c.Do(req)
if err != nil {
return sectionsSubnetsData, err
}
err = json.Unmarshal([]byte(body), §ionsSubnetsData)
if err != nil {
return sectionsSubnetsData, err
}
if sectionsSubnetsData.Code != 200 {
return sectionsSubnetsData, errors.New(sectionsSubnetsData.Message)
}
return sectionsSubnetsData, nil
}