|
| 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