|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mockcompute |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http/httptest" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs" |
| 24 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" |
| 25 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors" |
| 26 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
| 27 | + "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" |
| 28 | + "k8s.io/kops/cloudmock/openstack" |
| 29 | +) |
| 30 | + |
| 31 | +// MockClient represents a mocked networks (nebula) client |
| 32 | +type MockClient struct { |
| 33 | + openstack.MockOpenstackServer |
| 34 | + mutex sync.Mutex |
| 35 | + |
| 36 | + serverGroups map[string]servergroups.ServerGroup |
| 37 | + servers map[string]servers.Server |
| 38 | + keyPairs map[string]keypairs.KeyPair |
| 39 | + images map[string]images.Image |
| 40 | + flavors map[string]flavors.Flavor |
| 41 | +} |
| 42 | + |
| 43 | +// CreateClient will create a new mock networking client |
| 44 | +func CreateClient() *MockClient { |
| 45 | + m := &MockClient{} |
| 46 | + m.SetupMux() |
| 47 | + m.Reset() |
| 48 | + m.mockServerGroups() |
| 49 | + m.mockServers() |
| 50 | + m.mockKeyPairs() |
| 51 | + m.mockImages() |
| 52 | + m.mockFlavors() |
| 53 | + m.Server = httptest.NewServer(m.Mux) |
| 54 | + return m |
| 55 | +} |
| 56 | + |
| 57 | +// Reset will empty the state of the mock data |
| 58 | +func (m *MockClient) Reset() { |
| 59 | + m.serverGroups = make(map[string]servergroups.ServerGroup) |
| 60 | + m.servers = make(map[string]servers.Server) |
| 61 | + m.keyPairs = make(map[string]keypairs.KeyPair) |
| 62 | + m.images = make(map[string]images.Image) |
| 63 | + m.flavors = make(map[string]flavors.Flavor) |
| 64 | +} |
| 65 | + |
| 66 | +// All returns a map of all resource IDs to their resources |
| 67 | +func (m *MockClient) All() map[string]interface{} { |
| 68 | + all := make(map[string]interface{}) |
| 69 | + for id, sg := range m.serverGroups { |
| 70 | + all[id] = sg |
| 71 | + } |
| 72 | + for id, kp := range m.keyPairs { |
| 73 | + all[id] = kp |
| 74 | + } |
| 75 | + for id, s := range m.servers { |
| 76 | + all[id] = s |
| 77 | + } |
| 78 | + return all |
| 79 | +} |
0 commit comments