Debian Codegraph @

localservices/cloudapi/service_fabrics.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "fmt"
5 "math/rand"
6
7 "github.com/joyent/gosdc/cloudapi"
8 "github.com/joyent/gosdc/localservices"
9 )
10
11 func (c *CloudAPI) getFabricWrapper(vlanID int16) (*fabricVLAN, error) {
12 vlan, present := c.fabricVLANs[vlanID]
13 if !present {
14 return nil, fmt.Errorf("VLAN %d not found", vlanID)
15 }
16
17 return vlan, nil
18 }
19
20 // ListFabricVLANs lists VLANs
21 func (c *CloudAPI) ListFabricVLANs() ([]cloudapi.FabricVLAN, error) {
22 out := []cloudapi.FabricVLAN{}
23 for _, vlan := range c.fabricVLANs {
24 out = append(out, vlan.FabricVLAN)
25 }
26
27 return out, nil
28 }
29
30 // GetFabricVLAN retrieves a single VLAN by ID
31 func (c *CloudAPI) GetFabricVLAN(vlanID int16) (*cloudapi.FabricVLAN, error) {
32 vlan, err := c.getFabricWrapper(vlanID)
33 if err != nil {
34 return nil, err
35 }
36
37 return &vlan.FabricVLAN, nil
38 }
39
40 // CreateFabricVLAN creates a new VLAN with the specified options
41 func (c *CloudAPI) CreateFabricVLAN(vlan cloudapi.FabricVLAN) (*cloudapi.FabricVLAN, error) {
42 id := int16(rand.Intn(4095 + 1))
43 vlan.Id = id
44
45 c.fabricVLANs[id] = &fabricVLAN{
46 FabricVLAN: vlan,
47 Networks: make(map[string]*cloudapi.FabricNetwork),
48 }
49
50 return &vlan, nil
51 }
52
53 // UpdateFabricVLAN updates a given VLAN with new fields
54 func (c *CloudAPI) UpdateFabricVLAN(new cloudapi.FabricVLAN) (*cloudapi.FabricVLAN, error) {
55 current, err := c.GetFabricVLAN(new.Id)
56 if err != nil {
57 return nil, err
58 }
59
60 current.Name = new.Name
61 current.Description = new.Description
62
63 return current, nil
64 }
65
66 // DeleteFabricVLAN delets a given VLAN as specified by ID
67 func (c *CloudAPI) DeleteFabricVLAN(vlanID int16) error {
68 _, present := c.fabricVLANs[vlanID]
69 if !present {
70 return fmt.Errorf("VLAN %d not found", vlanID)
71 }
72
73 delete(c.fabricVLANs, vlanID)
74 return nil
75 }
76
77 // ListFabricNetworks lists the networks inside the given VLAN
78 func (c *CloudAPI) ListFabricNetworks(vlanID int16) ([]cloudapi.FabricNetwork, error) {
79 vlan, err := c.getFabricWrapper(vlanID)
80 if err != nil {
81 return nil, err
82 }
83
84 out := []cloudapi.FabricNetwork{}
85 for _, network := range vlan.Networks {
86 out = append(out, *network)
87 }
88
89 return out, nil
90 }
91
92 // GetFabricNetwork gets a single network by VLAN and Network IDs
93 func (c *CloudAPI) GetFabricNetwork(vlanID int16, networkID string) (*cloudapi.FabricNetwork, error) {
94 vlan, err := c.getFabricWrapper(vlanID)
95 if err != nil {
96 return nil, err
97 }
98
99 network, present := vlan.Networks[networkID]
100 if !present {
101 return nil, fmt.Errorf("Network %s not found", networkID)
102 }
103
104 return network, nil
105 }
106
107 // CreateFabricNetwork creates a new fabric network
108 func (c *CloudAPI) CreateFabricNetwork(vlanID int16, opts cloudapi.CreateFabricNetworkOpts) (*cloudapi.FabricNetwork, error) {
109 id, err := localservices.NewUUID()
110 if err != nil {
111 return nil, err
112 }
113
114 vlan, err := c.getFabricWrapper(vlanID)
115 if err != nil {
116 return nil, err
117 }
118
119 vlan.Networks[id] = &cloudapi.FabricNetwork{
120 Id: id,
121 Name: opts.Name,
122 Public: false,
123 Fabric: true,
124 Description: opts.Description,
125 Subnet: opts.Subnet,
126 ProvisionStartIp: opts.ProvisionStartIp,
127 ProvisionEndIp: opts.ProvisionEndIp,
128 Gateway: opts.Gateway,
129 Routes: opts.Routes,
130 InternetNAT: opts.InternetNAT,
131 VLANId: vlanID,
132 }
133
134 return vlan.Networks[id], nil
135 }
136
137 // DeleteFabricNetwork deletes an existing fabric network
138 func (c *CloudAPI) DeleteFabricNetwork(vlanID int16, networkID string) error {
139 vlan, err := c.getFabricWrapper(vlanID)
140 if err != nil {
141 return err
142 }
143
144 _, present := vlan.Networks[networkID]
145 if !present {
146 return fmt.Errorf("Network %s not found", networkID)
147 }
148
149 delete(vlan.Networks, networkID)
150 return nil
151 }