|
| 1 | +/* |
| 2 | +Copyright 2015 The Kubernetes Authors All rights reserved. |
| 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 testclient |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/GoogleCloudPlatform/kubernetes/pkg/fields" |
| 23 | + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" |
| 24 | + "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" |
| 25 | +) |
| 26 | + |
| 27 | +func NewRootGetAction(resource, name string) GetActionImpl { |
| 28 | + action := GetActionImpl{} |
| 29 | + action.Verb = "get" |
| 30 | + action.Resource = resource |
| 31 | + action.Name = name |
| 32 | + |
| 33 | + return action |
| 34 | +} |
| 35 | + |
| 36 | +func NewGetAction(resource, namespace, name string) GetActionImpl { |
| 37 | + action := GetActionImpl{} |
| 38 | + action.Verb = "get" |
| 39 | + action.Resource = resource |
| 40 | + action.Namespace = namespace |
| 41 | + action.Name = name |
| 42 | + |
| 43 | + return action |
| 44 | +} |
| 45 | + |
| 46 | +func NewRootListAction(resource string, label labels.Selector, field fields.Selector) ListActionImpl { |
| 47 | + action := ListActionImpl{} |
| 48 | + action.Verb = "list" |
| 49 | + action.Resource = resource |
| 50 | + action.ListRestrictions = ListRestrictions{label, field} |
| 51 | + |
| 52 | + return action |
| 53 | +} |
| 54 | + |
| 55 | +func NewListAction(resource, namespace string, label labels.Selector, field fields.Selector) ListActionImpl { |
| 56 | + action := ListActionImpl{} |
| 57 | + action.Verb = "list" |
| 58 | + action.Resource = resource |
| 59 | + action.Namespace = namespace |
| 60 | + action.ListRestrictions = ListRestrictions{label, field} |
| 61 | + |
| 62 | + return action |
| 63 | +} |
| 64 | + |
| 65 | +func NewRootCreateAction(resource string, object runtime.Object) CreateActionImpl { |
| 66 | + action := CreateActionImpl{} |
| 67 | + action.Verb = "create" |
| 68 | + action.Resource = resource |
| 69 | + action.Object = object |
| 70 | + |
| 71 | + return action |
| 72 | +} |
| 73 | + |
| 74 | +func NewCreateAction(resource, namespace string, object runtime.Object) CreateActionImpl { |
| 75 | + action := CreateActionImpl{} |
| 76 | + action.Verb = "create" |
| 77 | + action.Resource = resource |
| 78 | + action.Namespace = namespace |
| 79 | + action.Object = object |
| 80 | + |
| 81 | + return action |
| 82 | +} |
| 83 | + |
| 84 | +func NewRootUpdateAction(resource string, object runtime.Object) UpdateActionImpl { |
| 85 | + action := UpdateActionImpl{} |
| 86 | + action.Verb = "update" |
| 87 | + action.Resource = resource |
| 88 | + action.Object = object |
| 89 | + |
| 90 | + return action |
| 91 | +} |
| 92 | + |
| 93 | +func NewUpdateAction(resource, namespace string, object runtime.Object) UpdateActionImpl { |
| 94 | + action := UpdateActionImpl{} |
| 95 | + action.Verb = "update" |
| 96 | + action.Resource = resource |
| 97 | + action.Namespace = namespace |
| 98 | + action.Object = object |
| 99 | + |
| 100 | + return action |
| 101 | +} |
| 102 | + |
| 103 | +func NewRootDeleteAction(resource, name string) DeleteActionImpl { |
| 104 | + action := DeleteActionImpl{} |
| 105 | + action.Verb = "delete" |
| 106 | + action.Resource = resource |
| 107 | + action.Name = name |
| 108 | + |
| 109 | + return action |
| 110 | +} |
| 111 | + |
| 112 | +func NewDeleteAction(resource, namespace, name string) DeleteActionImpl { |
| 113 | + action := DeleteActionImpl{} |
| 114 | + action.Verb = "delete" |
| 115 | + action.Resource = resource |
| 116 | + action.Namespace = namespace |
| 117 | + action.Name = name |
| 118 | + |
| 119 | + return action |
| 120 | +} |
| 121 | + |
| 122 | +func NewRootWatchAction(resource string, label labels.Selector, field fields.Selector, resourceVersion string) WatchActionImpl { |
| 123 | + action := WatchActionImpl{} |
| 124 | + action.Verb = "watch" |
| 125 | + action.Resource = resource |
| 126 | + action.WatchRestrictions = WatchRestrictions{label, field, resourceVersion} |
| 127 | + |
| 128 | + return action |
| 129 | +} |
| 130 | + |
| 131 | +func NewWatchAction(resource, namespace string, label labels.Selector, field fields.Selector, resourceVersion string) WatchActionImpl { |
| 132 | + action := WatchActionImpl{} |
| 133 | + action.Verb = "watch" |
| 134 | + action.Resource = resource |
| 135 | + action.Namespace = namespace |
| 136 | + action.WatchRestrictions = WatchRestrictions{label, field, resourceVersion} |
| 137 | + |
| 138 | + return action |
| 139 | +} |
| 140 | + |
| 141 | +type ListRestrictions struct { |
| 142 | + Labels labels.Selector |
| 143 | + Fields fields.Selector |
| 144 | +} |
| 145 | +type WatchRestrictions struct { |
| 146 | + Labels labels.Selector |
| 147 | + Fields fields.Selector |
| 148 | + ResourceVersion string |
| 149 | +} |
| 150 | + |
| 151 | +type Action interface { |
| 152 | + GetNamespace() string |
| 153 | + GetVerb() string |
| 154 | + GetResource() string |
| 155 | + GetSubresource() string |
| 156 | + Matches(verb, resource string) bool |
| 157 | +} |
| 158 | + |
| 159 | +type GenericAction interface { |
| 160 | + Action |
| 161 | + GetValue() interface{} |
| 162 | +} |
| 163 | + |
| 164 | +type GetAction interface { |
| 165 | + Action |
| 166 | + GetName() string |
| 167 | +} |
| 168 | + |
| 169 | +type ListAction interface { |
| 170 | + Action |
| 171 | + GetListRestrictions() ListRestrictions |
| 172 | +} |
| 173 | + |
| 174 | +type CreateAction interface { |
| 175 | + Action |
| 176 | + GetObject() runtime.Object |
| 177 | +} |
| 178 | + |
| 179 | +type UpdateAction interface { |
| 180 | + Action |
| 181 | + GetObject() runtime.Object |
| 182 | +} |
| 183 | + |
| 184 | +type DeleteAction interface { |
| 185 | + Action |
| 186 | + GetName() string |
| 187 | +} |
| 188 | + |
| 189 | +type WatchAction interface { |
| 190 | + Action |
| 191 | + GetWatchRestrictions() WatchRestrictions |
| 192 | +} |
| 193 | + |
| 194 | +type ActionImpl struct { |
| 195 | + Namespace string |
| 196 | + Verb string |
| 197 | + Resource string |
| 198 | + Subresource string |
| 199 | +} |
| 200 | + |
| 201 | +func (a ActionImpl) GetNamespace() string { |
| 202 | + return a.Namespace |
| 203 | +} |
| 204 | +func (a ActionImpl) GetVerb() string { |
| 205 | + return a.Verb |
| 206 | +} |
| 207 | +func (a ActionImpl) GetResource() string { |
| 208 | + return a.Resource |
| 209 | +} |
| 210 | +func (a ActionImpl) GetSubresource() string { |
| 211 | + return a.Subresource |
| 212 | +} |
| 213 | +func (a ActionImpl) Matches(verb, resource string) bool { |
| 214 | + return strings.ToLower(verb) == strings.ToLower(a.Verb) && |
| 215 | + strings.ToLower(resource) == strings.ToLower(a.Resource) |
| 216 | +} |
| 217 | + |
| 218 | +type GenericActionImpl struct { |
| 219 | + ActionImpl |
| 220 | + Value interface{} |
| 221 | +} |
| 222 | + |
| 223 | +func (a GenericActionImpl) GetValue() interface{} { |
| 224 | + return a.Value |
| 225 | +} |
| 226 | + |
| 227 | +type GetActionImpl struct { |
| 228 | + ActionImpl |
| 229 | + Name string |
| 230 | +} |
| 231 | + |
| 232 | +func (a GetActionImpl) GetName() string { |
| 233 | + return a.Name |
| 234 | +} |
| 235 | + |
| 236 | +type ListActionImpl struct { |
| 237 | + ActionImpl |
| 238 | + ListRestrictions ListRestrictions |
| 239 | +} |
| 240 | + |
| 241 | +func (a ListActionImpl) GetListRestrictions() ListRestrictions { |
| 242 | + return a.ListRestrictions |
| 243 | +} |
| 244 | + |
| 245 | +type CreateActionImpl struct { |
| 246 | + ActionImpl |
| 247 | + Object runtime.Object |
| 248 | +} |
| 249 | + |
| 250 | +func (a CreateActionImpl) GetObject() runtime.Object { |
| 251 | + return a.Object |
| 252 | +} |
| 253 | + |
| 254 | +type UpdateActionImpl struct { |
| 255 | + ActionImpl |
| 256 | + Object runtime.Object |
| 257 | +} |
| 258 | + |
| 259 | +func (a UpdateActionImpl) GetObject() runtime.Object { |
| 260 | + return a.Object |
| 261 | +} |
| 262 | + |
| 263 | +type DeleteActionImpl struct { |
| 264 | + ActionImpl |
| 265 | + Name string |
| 266 | +} |
| 267 | + |
| 268 | +func (a DeleteActionImpl) GetName() string { |
| 269 | + return a.Name |
| 270 | +} |
| 271 | + |
| 272 | +type WatchActionImpl struct { |
| 273 | + ActionImpl |
| 274 | + WatchRestrictions WatchRestrictions |
| 275 | +} |
| 276 | + |
| 277 | +func (a WatchActionImpl) GetWatchRestrictions() WatchRestrictions { |
| 278 | + return a.WatchRestrictions |
| 279 | +} |
0 commit comments