Skip to content

Commit 1202c3b

Browse files
committed
Create FloatingIP
1 parent cf6de0c commit 1202c3b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

cloudmock/openstack/mocknetworking/floatingips.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,29 @@ import (
2323
"net/url"
2424
"regexp"
2525

26+
"github.com/google/uuid"
2627
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
2728
)
2829

2930
type floatingIPListResponse struct {
3031
FloatingIPs []floatingips.FloatingIP `json:"floatingips"`
3132
}
3233

34+
type floatingIPType struct {
35+
ID string `json:"id"`
36+
FloatingIP string `json:"floatingip"`
37+
TenantID string `json:"tenant_id"`
38+
ProjectID string `json:"project_id"`
39+
}
40+
41+
type floatingIPCreateRequest struct {
42+
FloatingIP floatingIPType `json:"floatingip"`
43+
}
44+
45+
type floatingIPGetResponse struct {
46+
FloatingIP floatingips.FloatingIP `json:"floatingip"`
47+
}
48+
3349
func (m *MockClient) mockFloatingIPs() {
3450
re := regexp.MustCompile(`/floatingips/?`)
3551

@@ -46,6 +62,8 @@ func (m *MockClient) mockFloatingIPs() {
4662
r.ParseForm()
4763
m.listFloatingIPs(w, r.Form)
4864
}
65+
case http.MethodPost:
66+
m.createFloatingIp(w, r)
4967
default:
5068
w.WriteHeader(http.StatusBadRequest)
5169
}
@@ -73,3 +91,39 @@ func (m *MockClient) listFloatingIPs(w http.ResponseWriter, vals url.Values) {
7391
panic("failed to write body")
7492
}
7593
}
94+
95+
func (m *MockClient) createFloatingIp(w http.ResponseWriter, r *http.Request) {
96+
var create floatingIPCreateRequest
97+
err := json.NewDecoder(r.Body).Decode(&create)
98+
if err != nil {
99+
panic("error decoding create floating ip request")
100+
}
101+
102+
w.WriteHeader(http.StatusAccepted)
103+
104+
f := floatingips.FloatingIP{
105+
ID: uuid.New().String(),
106+
FloatingIP: create.FloatingIP.FloatingIP,
107+
TenantID: create.FloatingIP.TenantID,
108+
//UpdatedAt: time.Now(),
109+
//CreatedAt: time.Now(),
110+
ProjectID: create.FloatingIP.ProjectID,
111+
Status: "ACTIVE",
112+
RouterID: "router",
113+
Tags: nil,
114+
RevisionNumber: 0,
115+
}
116+
m.floatingips[f.ID] = f
117+
118+
resp := floatingIPGetResponse{
119+
FloatingIP: f,
120+
}
121+
respB, err := json.Marshal(resp)
122+
if err != nil {
123+
panic(fmt.Sprintf("failed to marshal %+v", resp))
124+
}
125+
_, err = w.Write(respB)
126+
if err != nil {
127+
panic("failed to write body")
128+
}
129+
}

0 commit comments

Comments
 (0)