yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/test/readonly.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	"os"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/log"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/onecloud/pkg/util/printutils"
    26  	"yunion.io/x/onecloud/pkg/util/shellutils"
    27  )
    28  
    29  func TestShell() {
    30  
    31  	type RegionResourceCount struct {
    32  		ZoneCount             int
    33  		VpcCount              int
    34  		NetworkCount          int
    35  		EipCount              int
    36  		VmCount               int
    37  		LbCount               int
    38  		LbCertCount           int
    39  		DiskCount             int
    40  		SnapshotCount         int
    41  		SnapshotPolicyCount   int
    42  		ImageCount            int
    43  		NetworkInterfaceCount int
    44  		BucketCount           int
    45  		RdsCount              int
    46  	}
    47  
    48  	type ReadonlyTestOptions struct {
    49  		TestVpc bool `default:"true"`
    50  		TestLb  bool `default:"true"`
    51  	}
    52  
    53  	var list = func(parent cloudprovider.ICloudResource, resource string, callback func() (interface{}, error)) interface{} {
    54  		result, err := callback()
    55  		if err != nil {
    56  			if errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {
    57  				return result
    58  			}
    59  			log.Errorf("list %s error: %v", resource, err)
    60  			os.Exit(-1)
    61  		}
    62  		log.Debugf("%s(%s) %s:", parent.GetName(), parent.GetGlobalId(), resource)
    63  		printutils.PrintGetterList(result, nil)
    64  		return result
    65  	}
    66  
    67  	var show = func(parent cloudprovider.ICloudResource, resource string, callback func() (interface{}, error)) interface{} {
    68  		result, err := callback()
    69  		if err != nil {
    70  			if errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {
    71  				return result
    72  			}
    73  			log.Errorf("show %s(%s) %s error: %v", parent.GetName(), parent.GetGlobalId(), resource, err)
    74  			os.Exit(-1)
    75  		}
    76  		log.Debugf("%s(%s) %s:", parent.GetName(), parent.GetGlobalId(), resource)
    77  		printutils.PrintGetterObject(result)
    78  		return result
    79  	}
    80  
    81  	shellutils.R(&ReadonlyTestOptions{}, "test-readonly", "Test read", func(cli cloudprovider.ICloudRegion, args *ReadonlyTestOptions) error {
    82  		result := RegionResourceCount{
    83  			ZoneCount:             0,
    84  			VpcCount:              0,
    85  			NetworkCount:          0,
    86  			EipCount:              0,
    87  			VmCount:               0,
    88  			LbCount:               0,
    89  			LbCertCount:           0,
    90  			DiskCount:             0,
    91  			SnapshotCount:         0,
    92  			SnapshotPolicyCount:   0,
    93  			ImageCount:            0,
    94  			NetworkInterfaceCount: 0,
    95  			BucketCount:           0,
    96  		}
    97  		if args.TestVpc {
    98  			_vpcs := list(cli, "vpcs", func() (interface{}, error) {
    99  				return cli.GetIVpcs()
   100  			})
   101  			vpcs := _vpcs.([]cloudprovider.ICloudVpc)
   102  			result.VpcCount += len(vpcs)
   103  			for i := range vpcs {
   104  				_wire := list(vpcs[i], "wire", func() (interface{}, error) {
   105  					return vpcs[i].GetIWires()
   106  				})
   107  				wires := _wire.([]cloudprovider.ICloudWire)
   108  				for j := range wires {
   109  					_networks := list(wires[j], "networks", func() (interface{}, error) {
   110  						return wires[j].GetINetworks()
   111  					})
   112  					networks := _networks.([]cloudprovider.ICloudNetwork)
   113  					result.NetworkCount += len(networks)
   114  				}
   115  			}
   116  		}
   117  		_eips := list(cli, "eips", func() (interface{}, error) {
   118  			return cli.GetIEips()
   119  		})
   120  		eips := _eips.([]cloudprovider.ICloudEIP)
   121  		result.EipCount = len(eips)
   122  		_snapshots := list(cli, "snapshots", func() (interface{}, error) {
   123  			return cli.GetISnapshots()
   124  		})
   125  		snapshots := _snapshots.([]cloudprovider.ICloudSnapshot)
   126  		result.SnapshotCount = len(snapshots)
   127  		_snapshotPolicies := list(cli, "snapshot policies", func() (interface{}, error) {
   128  			return cli.GetISnapshotPolicies()
   129  		})
   130  		snapshotPolicies := _snapshotPolicies.([]cloudprovider.ICloudSnapshotPolicy)
   131  		result.SnapshotPolicyCount = len(snapshotPolicies)
   132  		_nics := list(cli, "network interfaces", func() (interface{}, error) {
   133  			return cli.GetINetworkInterfaces()
   134  		})
   135  		nics := _nics.([]cloudprovider.ICloudNetworkInterface)
   136  		result.NetworkInterfaceCount = len(nics)
   137  
   138  		_buckets := list(cli, "buckets", func() (interface{}, error) {
   139  			return cli.GetIBuckets()
   140  		})
   141  		buckets := _buckets.([]cloudprovider.ICloudBucket)
   142  		result.BucketCount = len(buckets)
   143  		for i := range buckets {
   144  			list(buckets[i], "bucket objects", func() (interface{}, error) {
   145  				return buckets[i].ListObjects("", "", "", 10)
   146  			})
   147  		}
   148  
   149  		list(cli, "storages", func() (interface{}, error) {
   150  			return cli.GetIStorages()
   151  		})
   152  		_caches := list(cli, "storagecaches", func() (interface{}, error) {
   153  			return cli.GetIStoragecaches()
   154  		})
   155  		caches := _caches.([]cloudprovider.ICloudStoragecache)
   156  		for i := range caches {
   157  			_images := list(caches[i], "images", func() (interface{}, error) {
   158  				return caches[i].GetICloudImages()
   159  			})
   160  			images := _images.([]cloudprovider.ICloudImage)
   161  			result.ImageCount = len(images)
   162  		}
   163  
   164  		_zones := list(cli, "zones", func() (interface{}, error) {
   165  			return cli.GetIZones()
   166  		})
   167  		zones := _zones.([]cloudprovider.ICloudZone)
   168  		result.ZoneCount = len(zones)
   169  		for i := range zones {
   170  			_hosts := list(zones[i], "host", func() (interface{}, error) {
   171  				return zones[i].GetIHosts()
   172  			})
   173  			hosts := _hosts.([]cloudprovider.ICloudHost)
   174  			for j := range hosts {
   175  				_vms := list(hosts[j], "vms", func() (interface{}, error) {
   176  					return hosts[j].GetIVMs()
   177  				})
   178  				vms := _vms.([]cloudprovider.ICloudVM)
   179  				result.VmCount += len(vms)
   180  				for k := range vms {
   181  					list(vms[k], "vm disks", func() (interface{}, error) {
   182  						return vms[k].GetIDisks()
   183  					})
   184  					list(vms[k], "vm nics", func() (interface{}, error) {
   185  						return vms[k].GetINics()
   186  					})
   187  					show(vms[k], "vm eip", func() (interface{}, error) {
   188  						return vms[k].GetIEIP()
   189  					})
   190  				}
   191  			}
   192  			_storages := list(zones[i], "storages", func() (interface{}, error) {
   193  				return zones[i].GetIStorages()
   194  			})
   195  			storages := _storages.([]cloudprovider.ICloudStorage)
   196  			for j := range storages {
   197  				_disks := list(storages[j], "disks", func() (interface{}, error) {
   198  					return storages[j].GetIDisks()
   199  				})
   200  				disks := _disks.([]cloudprovider.ICloudDisk)
   201  				result.DiskCount += len(disks)
   202  			}
   203  		}
   204  		if args.TestLb {
   205  			/*
   206  				list(cli, "lb acls", func() (interface{}, error) {
   207  					return cli.GetILoadBalancerAcls()
   208  				})
   209  			*/
   210  			_certs := list(cli, "lb certificates", func() (interface{}, error) {
   211  				return cli.GetILoadBalancerCertificates()
   212  			})
   213  			certs := _certs.([]cloudprovider.ICloudLoadbalancerCertificate)
   214  			result.LbCertCount = len(certs)
   215  			/*
   216  				list(cli, "lb backend groups", func() (interface{}, error) {
   217  					return cli.GetILoadBalancerBackendGroups()
   218  				})
   219  			*/
   220  			_lbs := list(cli, "lbs", func() (interface{}, error) {
   221  				return cli.GetILoadBalancers()
   222  			})
   223  			lbs := _lbs.([]cloudprovider.ICloudLoadbalancer)
   224  			result.LbCount += len(lbs)
   225  			for i := range lbs {
   226  				_listeners := list(lbs[i], "lb listeners", func() (interface{}, error) {
   227  					return lbs[i].GetILoadBalancerListeners()
   228  				})
   229  				listeners := _listeners.([]cloudprovider.ICloudLoadbalancerListener)
   230  				for j := range listeners {
   231  					list(listeners[j], "listener rules", func() (interface{}, error) {
   232  						return listeners[j].GetILoadbalancerListenerRules()
   233  					})
   234  				}
   235  				_groups := list(lbs[i], "lb backend groups", func() (interface{}, error) {
   236  					return lbs[i].GetILoadBalancerBackendGroups()
   237  				})
   238  				groups := _groups.([]cloudprovider.ICloudLoadbalancerBackendGroup)
   239  				for j := range groups {
   240  					list(groups[j], "lb backend", func() (interface{}, error) {
   241  						return groups[j].GetILoadbalancerBackends()
   242  					})
   243  				}
   244  			}
   245  		}
   246  
   247  		_rds := list(cli, "rds", func() (interface{}, error) {
   248  			return cli.GetIDBInstances()
   249  		})
   250  		rds := _rds.([]cloudprovider.ICloudDBInstance)
   251  		result.RdsCount = len(rds)
   252  		log.Println("resource sum for ", cli.GetName(), jsonutils.Marshal(result).PrettyString())
   253  		return nil
   254  	})
   255  }