|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package migrate |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + |
| 23 | + "github.com/apisix/manager-api/internal/core/entity" |
| 24 | + "github.com/apisix/manager-api/internal/core/store" |
| 25 | +) |
| 26 | + |
| 27 | +type DataSet struct { |
| 28 | + Counsumers []*entity.Consumer |
| 29 | + Routes []*entity.Route |
| 30 | + Services []*entity.Service |
| 31 | + SSLs []*entity.SSL |
| 32 | + Upstreams []*entity.Upstream |
| 33 | + Scripts []*entity.Script |
| 34 | + GlobalPlugins []*entity.GlobalPlugins |
| 35 | + PluginConfigs []*entity.PluginConfig |
| 36 | +} |
| 37 | + |
| 38 | +func newDataSet() *DataSet { |
| 39 | + return &DataSet{ |
| 40 | + Counsumers: make([]*entity.Consumer, 0), |
| 41 | + Routes: make([]*entity.Route, 0), |
| 42 | + Services: make([]*entity.Service, 0), |
| 43 | + SSLs: make([]*entity.SSL, 0), |
| 44 | + Upstreams: make([]*entity.Upstream, 0), |
| 45 | + Scripts: make([]*entity.Script, 0), |
| 46 | + GlobalPlugins: make([]*entity.GlobalPlugins, 0), |
| 47 | + PluginConfigs: make([]*entity.PluginConfig, 0), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (a *DataSet) rangeData(key store.HubKey, f func(int, interface{}) bool) { |
| 52 | + switch key { |
| 53 | + case store.HubKeyConsumer: |
| 54 | + for i, v := range a.Counsumers { |
| 55 | + if !f(i, v) { |
| 56 | + break |
| 57 | + } |
| 58 | + } |
| 59 | + case store.HubKeyRoute: |
| 60 | + for i, v := range a.Routes { |
| 61 | + if !f(i, v) { |
| 62 | + break |
| 63 | + } |
| 64 | + } |
| 65 | + case store.HubKeyService: |
| 66 | + for i, v := range a.Services { |
| 67 | + if !f(i, v) { |
| 68 | + break |
| 69 | + } |
| 70 | + } |
| 71 | + case store.HubKeySsl: |
| 72 | + for i, v := range a.SSLs { |
| 73 | + if !f(i, v) { |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + case store.HubKeyUpstream: |
| 78 | + for i, v := range a.Upstreams { |
| 79 | + if !f(i, v) { |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + case store.HubKeyScript: |
| 84 | + for i, v := range a.Scripts { |
| 85 | + if !f(i, v) { |
| 86 | + break |
| 87 | + } |
| 88 | + } |
| 89 | + case store.HubKeyGlobalRule: |
| 90 | + for i, v := range a.GlobalPlugins { |
| 91 | + if !f(i, v) { |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + case store.HubKeyPluginConfig: |
| 96 | + for i, v := range a.PluginConfigs { |
| 97 | + if !f(i, v) { |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func (a *DataSet) Add(obj interface{}) error { |
| 105 | + var err error = nil |
| 106 | + switch obj := obj.(type) { |
| 107 | + case *entity.Consumer: |
| 108 | + a.Counsumers = append(a.Counsumers, obj) |
| 109 | + case *entity.Route: |
| 110 | + a.Routes = append(a.Routes, obj) |
| 111 | + case *entity.Service: |
| 112 | + a.Services = append(a.Services, obj) |
| 113 | + case *entity.SSL: |
| 114 | + a.SSLs = append(a.SSLs, obj) |
| 115 | + case *entity.Upstream: |
| 116 | + a.Upstreams = append(a.Upstreams, obj) |
| 117 | + case *entity.Script: |
| 118 | + a.Scripts = append(a.Scripts, obj) |
| 119 | + case *entity.GlobalPlugins: |
| 120 | + a.GlobalPlugins = append(a.GlobalPlugins, obj) |
| 121 | + case *entity.PluginConfig: |
| 122 | + a.PluginConfigs = append(a.PluginConfigs, obj) |
| 123 | + default: |
| 124 | + err = errors.New("Unknown type of obj") |
| 125 | + } |
| 126 | + return err |
| 127 | +} |
0 commit comments