Skip to content

Extend openstack mock by misc. resources #17406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions cloudmock/openstack/mockcompute/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"net/http/httptest"
"sync"

"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/instanceactions"

"github.com/gophercloud/gophercloud/v2"

"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/flavors"
Expand All @@ -35,12 +37,13 @@ type MockClient struct {
openstack.MockOpenstackServer
mutex sync.Mutex

serverGroups map[string]servergroups.ServerGroup
servers map[string]servers.Server
keyPairs map[string]keypairs.KeyPair
images map[string]images.Image
flavors map[string]flavors.Flavor
networkClient *gophercloud.ServiceClient
serverGroups map[string]servergroups.ServerGroup
servers map[string]servers.Server
keyPairs map[string]keypairs.KeyPair
images map[string]images.Image
flavors map[string]flavors.Flavor
instanceActions map[string]instanceactions.InstanceAction
networkClient *gophercloud.ServiceClient
}

// CreateClient will create a new mock networking client
Expand All @@ -52,6 +55,7 @@ func CreateClient(networkClient *gophercloud.ServiceClient) *MockClient {
m.mockServers()
m.mockKeyPairs()
m.mockFlavors()
m.mockInstanceActions()
m.Server = httptest.NewServer(m.Mux)
m.networkClient = networkClient
return m
Expand Down
55 changes: 55 additions & 0 deletions cloudmock/openstack/mockcompute/instanceactions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mockcompute

import (
"encoding/json"
"net/http"
)

type instanceActionsResponse struct {
InstanceActions []interface{} `json:"instanceActions"`
}

func (m *MockClient) mockInstanceActions() {
//re := regexp.MustCompile(`/servers/(.*?)/os-instance-actions/?`)

handler := func(w http.ResponseWriter, r *http.Request) {
m.mutex.Lock()
defer m.mutex.Unlock()

w.Header().Add("Content-Type", "application/json")

if r.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
return
}

resp := instanceActionsResponse{
InstanceActions: make([]interface{}, 0),
}
respB, err := json.Marshal(resp)
if err != nil {
panic("failed to marshal response")
}
_, err = w.Write(respB)
if err != nil {
panic("failed to write body")
}
}
m.Mux.HandleFunc("/servers/{server_id}/os-instance-actions/", handler)
}
10 changes: 10 additions & 0 deletions cloudmock/openstack/mockcompute/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,21 @@ func (m *MockClient) createServer(w http.ResponseWriter, r *http.Request) {

w.WriteHeader(http.StatusAccepted)

flavorId := create.Server.FlavorRef
flavor := m.flavors[flavorId]

server := servers.Server{
ID: uuid.New().String(),
Name: create.Server.Name,
Metadata: create.Server.Metadata,
Status: "ACTIVE",
Flavor: map[string]any{
"id": flavor.ID,
"name": flavor.Name,
"ram": flavor.RAM,
"vcpus": flavor.VCPUs,
"disk": flavor.Disk,
},
}
securityGroups := make([]map[string]interface{}, len(create.Server.SecurityGroups))
for i, groupName := range create.Server.SecurityGroups {
Expand Down
54 changes: 54 additions & 0 deletions cloudmock/openstack/mocknetworking/floatingips.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,29 @@ import (
"net/url"
"regexp"

"github.com/google/uuid"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
)

type floatingIPListResponse struct {
FloatingIPs []floatingips.FloatingIP `json:"floatingips"`
}

type floatingIPType struct {
ID string `json:"id"`
FloatingIP string `json:"floatingip"`
TenantID string `json:"tenant_id"`
ProjectID string `json:"project_id"`
}

type floatingIPCreateRequest struct {
FloatingIP floatingIPType `json:"floatingip"`
}

type floatingIPGetResponse struct {
FloatingIP floatingips.FloatingIP `json:"floatingip"`
}

func (m *MockClient) mockFloatingIPs() {
re := regexp.MustCompile(`/floatingips/?`)

Expand All @@ -46,6 +62,8 @@ func (m *MockClient) mockFloatingIPs() {
r.ParseForm()
m.listFloatingIPs(w, r.Form)
}
case http.MethodPost:
m.createFloatingIp(w, r)
default:
w.WriteHeader(http.StatusBadRequest)
}
Expand Down Expand Up @@ -73,3 +91,39 @@ func (m *MockClient) listFloatingIPs(w http.ResponseWriter, vals url.Values) {
panic("failed to write body")
}
}

func (m *MockClient) createFloatingIp(w http.ResponseWriter, r *http.Request) {
var create floatingIPCreateRequest
err := json.NewDecoder(r.Body).Decode(&create)
if err != nil {
panic("error decoding create floating ip request")
}

w.WriteHeader(http.StatusAccepted)

f := floatingips.FloatingIP{
ID: uuid.New().String(),
FloatingIP: create.FloatingIP.FloatingIP,
TenantID: create.FloatingIP.TenantID,
//UpdatedAt: time.Now(),
//CreatedAt: time.Now(),
ProjectID: create.FloatingIP.ProjectID,
Status: "ACTIVE",
RouterID: "router",
Tags: nil,
RevisionNumber: 0,
}
m.floatingips[f.ID] = f

resp := floatingIPGetResponse{
FloatingIP: f,
}
respB, err := json.Marshal(resp)
if err != nil {
panic(fmt.Sprintf("failed to marshal %+v", resp))
}
_, err = w.Write(respB)
if err != nil {
panic("failed to write body")
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ require (
github.com/google/go-tpm v0.9.3
github.com/google/go-tpm-tools v0.4.5
github.com/google/uuid v1.6.0
github.com/gophercloud/gophercloud/v2 v2.6.0
github.com/gophercloud/gophercloud/v2 v2.7.0
github.com/hetznercloud/hcloud-go v1.59.2
github.com/jacksontj/memberlistmesh v0.0.0-20190905163944-93462b9d2bb7
github.com/pelletier/go-toml v1.9.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU
github.com/googleapis/enterprise-certificate-proxy v0.3.6/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA=
github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q=
github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA=
github.com/gophercloud/gophercloud/v2 v2.6.0 h1:XJKQ0in3iHOZHVAFMXq/OhjCuvvG+BKR0unOqRfG1EI=
github.com/gophercloud/gophercloud/v2 v2.6.0/go.mod h1:Ki/ILhYZr/5EPebrPL9Ej+tUg4lqx71/YH2JWVeU+Qk=
github.com/gophercloud/gophercloud/v2 v2.7.0 h1:o0m4kgVcPgHlcXiWAjoVxGd8QCmvM5VU+YM71pFbn0E=
github.com/gophercloud/gophercloud/v2 v2.7.0/go.mod h1:Ki/ILhYZr/5EPebrPL9Ej+tUg4lqx71/YH2JWVeU+Qk=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/gophercloud/gophercloud/v2 v2.6.0 // indirect
github.com/gophercloud/gophercloud/v2 v2.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q=
github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA=
github.com/gophercloud/gophercloud/v2 v2.6.0 h1:XJKQ0in3iHOZHVAFMXq/OhjCuvvG+BKR0unOqRfG1EI=
github.com/gophercloud/gophercloud/v2 v2.6.0/go.mod h1:Ki/ILhYZr/5EPebrPL9Ej+tUg4lqx71/YH2JWVeU+Qk=
github.com/gophercloud/gophercloud/v2 v2.7.0 h1:o0m4kgVcPgHlcXiWAjoVxGd8QCmvM5VU+YM71pFbn0E=
github.com/gophercloud/gophercloud/v2 v2.7.0/go.mod h1:Ki/ILhYZr/5EPebrPL9Ej+tUg4lqx71/YH2JWVeU+Qk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
Expand Down
11 changes: 11 additions & 0 deletions vendor/github.com/gophercloud/gophercloud/v2/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 59 additions & 2 deletions vendor/github.com/gophercloud/gophercloud/v2/endpoint_search.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading