Skip to content

Commit 4fdd852

Browse files
authored
Merge pull request #10 from Kubernetli/kkl_init
Kkl init
2 parents 6300c36 + abbaf8f commit 4fdd852

File tree

5 files changed

+233
-3
lines changed

5 files changed

+233
-3
lines changed

cmd/fmcCLI/args.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ type Args struct {
1414
Function string
1515
Cert string
1616
Input string
17+
Timeout string
1718
}
1819

1920
// reads/parses user input .
2021
func args() *Args {
2122
urlPtr := flag.String("url", "https://fmc/api", "url to FPM api")
2223
flag.StringVar(urlPtr, "u", "https://fmc/api", "url to FPM api")
2324
domainPtr := flag.String("domain", "global", "domain id")
24-
functionPtr := flag.String("function", "GetNetworks", "possible GetNetworks")
25+
functionPtr := flag.String("function", "GetNetworks", "possible GetNetworks|GetNetworkGroups|CreateNetworks|CreateNetworkGroups|UpdateNetworks|UpdateNetworkGroups")
2526
inputPtr := flag.String("input", "", "function Input in json")
2627
userPtr := flag.String("user", "admin", "API Username")
2728
pwPtr := flag.String("pw", "admin", "Username Password")
2829
fmcCertPtr := flag.String("cert", "", "adding x509 Certificate if client does not trust the fmc certificate")
30+
timeoutPtr := flag.String("timeout", "60", "timeout")
2931

3032
flag.Parse()
3133
return &Args{
@@ -36,5 +38,6 @@ func args() *Args {
3638
Function: *functionPtr,
3739
Cert: *fmcCertPtr,
3840
Input: *inputPtr,
41+
Timeout: *timeoutPtr,
3942
}
4043
}

cmd/fmcCLI/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/kubernetli/fmcClient/cmd/fmcCLI
22

33
go 1.14
4-
4+
//replace github.com/kubernetli/fmcClient/pkg/fmcClient => /home/chifu/go/src/github.com/kubernetli/fmcClient/pkg/fmcClient
55
require github.com/kubernetli/fmcClient/pkg/fmcClient v0.0.0-20200707130621-0142f917c9d1

cmd/fmcCLI/main.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"log"
8+
"time"
89

910
"github.com/kubernetli/fmcClient/pkg/fmcClient"
1011
)
@@ -24,12 +25,25 @@ func main() {
2425
}
2526
}
2627

28+
// read input file
29+
input, err := ioutil.ReadFile(arg.Input)
30+
if err != nil {
31+
if err.Error() == "open : no such file or directory" && arg.Input == "" {
32+
// do nothing
33+
} else {
34+
log.Fatal(err)
35+
}
36+
}
37+
2738
// create basic client and login
2839
t, err := fmcClient.NewClient(arg.user, arg.password, arg.baseURL, arg.Domain, cert)
2940
if err != nil {
3041
log.Fatal(err)
3142
}
3243

44+
//increase http timeout
45+
t.HTTPClient.Timeout = time.Minute * 5
46+
3347
// run function
3448
switch arg.Function {
3549
case "GetNetworks":
@@ -44,7 +58,7 @@ func main() {
4458
}
4559
fmt.Printf("%s\n", string(empJSON))
4660
case "CreateNetworks":
47-
res, err := t.CreateNetworks(arg.Input)
61+
res, err := t.CreateNetworks(string(input))
4862
if err != nil {
4963
log.Fatal(err)
5064
}
@@ -65,6 +79,27 @@ func main() {
6579
log.Fatal(err)
6680
}
6781
fmt.Printf("%s\n", string(empJSON))
82+
case "UpdateNetworkGroupsByObject":
83+
res, err := t.UpdateNetworkGroupsByObject(string(input))
84+
if err != nil {
85+
log.Fatal(err)
86+
}
87+
empJSON, err := json.MarshalIndent(res, "", " ")
88+
if err != nil {
89+
log.Fatal(err)
90+
}
91+
fmt.Printf("%s\n", string(empJSON))
92+
case "GetNetworkGroups":
93+
res, err := t.GetNetworkGroups()
94+
if err != nil {
95+
log.Fatal(err)
96+
}
97+
//pprint
98+
empJSON, err := json.MarshalIndent(res, "", " ")
99+
if err != nil {
100+
log.Fatal(err)
101+
}
102+
fmt.Printf("%s\n", string(empJSON))
68103
default:
69104
log.Fatalf("Function not known to me: %v", arg.Function)
70105
}

pkg/fmcClient/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func (c *Client) login() (*Client, error) {
9292
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
9393
req.Header.Set("Content-Type", "application/json; charset=utf-8")
9494
req.Header.Set("X-Auth-Access-Token", c.accessToken)
95+
//c.HTTPClient.Timeout
96+
//t.HTTPClient.Timeout = time.Second * 6000
9597

9698
res, err := c.HTTPClient.Do(req)
9799
if err != nil {

pkg/fmcClient/networkgroups.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package fmcClient
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"sort"
10+
"strings"
11+
)
12+
13+
// NetworkGroups .
14+
type NetworkGroups struct {
15+
Links struct {
16+
Self string `json:"self"`
17+
} `json:"links"`
18+
Items []struct {
19+
Links struct {
20+
Self string `json:"self"`
21+
} `json:"links"`
22+
Literals []struct {
23+
Type string `json:"type"`
24+
Value string `json:"value"`
25+
} `json:"literals"`
26+
Type string `json:"type"`
27+
Overridable bool `json:"overridable"`
28+
Description string `json:"description"`
29+
ID string `json:"id"`
30+
Name string `json:"name"`
31+
Metadata struct {
32+
ReadOnly struct {
33+
State bool `json:"state"`
34+
Reason string `json:"reason"`
35+
} `json:"UpdateNetworksreadOnly"`
36+
Timestamp int64 `json:"timestamp"`
37+
LastUser struct {
38+
Name string `json:"name"`
39+
} `json:"lastUser"`
40+
Domain struct {
41+
Name string `json:"name"`
42+
ID string `json:"id"`
43+
} `json:"domain"`
44+
} `json:"metadata"`
45+
} `json:"items"`
46+
Paging struct {
47+
Offset int `json:"offset"`
48+
Limit int `json:"limit"`
49+
Count int `json:"count"`
50+
Pages int `json:"pages"`
51+
} `json:"paging"`
52+
}
53+
54+
// NetworkGroupsInput .
55+
type NetworkGroupsInput struct {
56+
ID string `json:"id"`
57+
Name string `json:"name"`
58+
Type string `json:"type"`
59+
Objects []struct {
60+
Type string `json:"type"`
61+
ID string `json:"id"`
62+
} `json:"objects"`
63+
Literals []struct {
64+
Type string `json:"type"`
65+
Value string `json:"value"`
66+
} `json:"literals"`
67+
}
68+
69+
// GetNetworkGroups Get networkgroups from fmc
70+
func (c *Client) GetNetworkGroups() (*NetworkGroups, error) {
71+
// todo: implement limits
72+
// todo: implement filtering
73+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/fmc_config/v1/domain/%s/object/networkgroups?limit=10000&expanded=true", c.baseURL, c.Domain), nil)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
res := NetworkGroups{}
79+
// create Pointer for Network Struct
80+
err = c.sendRequest(req, &res)
81+
if err != nil {
82+
return nil, fmt.Errorf("failed %v", err)
83+
}
84+
return &res, nil
85+
}
86+
87+
// CreateNetworkGroups will create a Network from FMC
88+
func (c *Client) CreateNetworkGroups(i string) (*NetworkGroups, error) {
89+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/fmc_config/v1/domain/%s/object/networkgroups", c.baseURL, c.Domain), nil)
90+
if err != nil {
91+
return nil, err
92+
}
93+
var test NetworkGroupsInput
94+
//test that the json structur is OK
95+
err = json.Unmarshal([]byte(i), &test)
96+
if err != nil {
97+
return nil, fmt.Errorf("error found in input '%v' failed with '%v'", i, err)
98+
}
99+
100+
req.Body = ioutil.NopCloser(strings.NewReader(i))
101+
res := NetworkGroups{}
102+
// create Pointer for Network Struct
103+
err = c.sendRequest(req, &res)
104+
if err != nil {
105+
return nil, fmt.Errorf("failed %v", err)
106+
}
107+
return &res, nil
108+
}
109+
110+
// SearchNetworkGroups .
111+
func (c *Client) SearchNetworkGroups(network *NetworkGroupsInput, networks *NetworkGroups) (int, error) {
112+
var (
113+
err error
114+
)
115+
if err != nil {
116+
return 0, err
117+
}
118+
idx := sort.Search(len(networks.Items), func(i int) bool {
119+
return string(networks.Items[i].Name) >= network.Name
120+
})
121+
return idx, err
122+
}
123+
124+
// UpdateNetworkGroupsByObject will overwrite a Network Object
125+
func (c *Client) UpdateNetworkGroupsByObject(i string) (*NetworkGroups, error) {
126+
var (
127+
network NetworkGroupsInput
128+
err error
129+
res *NetworkGroups
130+
)
131+
json.Unmarshal([]byte(i), &network)
132+
networks, err := c.GetNetworkGroups()
133+
if err != nil {
134+
return nil, err
135+
}
136+
idx, err := c.SearchNetworkGroups(&network, networks)
137+
if err != nil {
138+
return nil, err
139+
}
140+
// Create Network if not exists
141+
if networks.Items[idx].Name != network.Name {
142+
// create new object
143+
c.CreateNetworkGroups(i)
144+
if err != nil {
145+
return nil, err
146+
}
147+
networks, err := c.GetNetworkGroups()
148+
if err != nil {
149+
return nil, err
150+
}
151+
152+
// Get Network list
153+
idx, err := c.SearchNetworkGroups(&network, networks)
154+
if err != nil {
155+
return nil, err
156+
}
157+
network.ID = networks.Items[idx].ID
158+
} else {
159+
network.ID = networks.Items[idx].ID
160+
}
161+
162+
res, err = c.UpdateNetworkGroups(network)
163+
if err != nil {
164+
return nil, err
165+
}
166+
//
167+
return res, err
168+
//
169+
}
170+
171+
// UpdateNetworkGroups will overwrite a Network Object
172+
func (c *Client) UpdateNetworkGroups(i NetworkGroupsInput) (*NetworkGroups, error) {
173+
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/fmc_config/v1/domain/%s/object/networkgroups/%s", c.baseURL, c.Domain, i.ID), nil)
174+
if err != nil {
175+
return nil, err
176+
}
177+
t, err := json.Marshal(i)
178+
if err != nil {
179+
return nil, err
180+
}
181+
req.Body = ioutil.NopCloser(bytes.NewReader(t))
182+
res := NetworkGroups{}
183+
184+
// create Pointer for Network Struct
185+
err = c.sendRequest(req, &res)
186+
if err != nil {
187+
return nil, fmt.Errorf("failed %v", err)
188+
}
189+
return &res, nil
190+
}

0 commit comments

Comments
 (0)