yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/shell/private_zone.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  // Copyright 2019 Yunion
    16  //
    17  // Licensed under the Apache License, Version 2.0 (the "License");
    18  // you may not use this file except in compliance with the License.
    19  // You may obtain a copy of the License at
    20  //
    21  //     http://www.apache.org/licenses/LICENSE-2.0
    22  //
    23  // Unless required by applicable law or agreed to in writing, software
    24  // distributed under the License is distributed on an "AS IS" BASIS,
    25  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    26  // See the License for the specific language governing permissions and
    27  // PageSizeations under the License.
    28  
    29  package shell
    30  
    31  import (
    32  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    33  	"yunion.io/x/cloudmux/pkg/multicloud/aliyun"
    34  	"yunion.io/x/onecloud/pkg/util/shellutils"
    35  )
    36  
    37  func init() {
    38  	type PrivateZoneListOptions struct {
    39  		PageSize   int `help:"page size"`
    40  		PageNumber int `help:"page PageNumber"`
    41  	}
    42  	shellutils.R(&PrivateZoneListOptions{}, "privatezone-list", "List privatezone", func(cli *aliyun.SRegion, args *PrivateZoneListOptions) error {
    43  		szones, e := cli.GetClient().DescribeZones(args.PageNumber, args.PageSize)
    44  		if e != nil {
    45  			return e
    46  		}
    47  		printList(szones.Zones.Zone, szones.TotalItems, args.PageNumber, args.PageSize, []string{})
    48  		return nil
    49  	})
    50  
    51  	type PrivateZoneCreateOptions struct {
    52  		NAME   string `help:"Domain name"`
    53  		Type   string `choices:"PublicZone|PrivateZone"`
    54  		Vpc    string `help:"vpc id"`
    55  		Region string `help:"region id"`
    56  	}
    57  	shellutils.R(&PrivateZoneCreateOptions{}, "privatezone-create", "Create privatezone", func(cli *aliyun.SRegion, args *PrivateZoneCreateOptions) error {
    58  		opts := cloudprovider.SDnsZoneCreateOptions{}
    59  		opts.Name = args.NAME
    60  		opts.ZoneType = cloudprovider.TDnsZoneType(args.Type)
    61  		if len(args.Vpc) > 0 && len(args.Region) > 0 {
    62  			vpc := cloudprovider.SPrivateZoneVpc{}
    63  			vpc.Id = args.Vpc
    64  			vpc.RegionId = args.Region
    65  			opts.Vpcs = []cloudprovider.SPrivateZoneVpc{vpc}
    66  		}
    67  		hostzones, err := cli.GetClient().CreateZone(&opts)
    68  		if err != nil {
    69  			return err
    70  		}
    71  		printObject(hostzones)
    72  		return nil
    73  	})
    74  
    75  	type PrivateZoneDeleteOptions struct {
    76  		PRIVATEZONEID string
    77  	}
    78  	shellutils.R(&PrivateZoneDeleteOptions{}, "privatezone-delete", "delete privatezone", func(cli *aliyun.SRegion, args *PrivateZoneDeleteOptions) error {
    79  		err := cli.GetClient().DeleteZone(args.PRIVATEZONEID)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		return nil
    84  	})
    85  
    86  	type PrivateZoneShowOptions struct {
    87  		ID string `help:"ID or name of privatezone"`
    88  	}
    89  	shellutils.R(&PrivateZoneShowOptions{}, "privatezone-show", "Show privatezone", func(cli *aliyun.SRegion, args *PrivateZoneShowOptions) error {
    90  		szone, e := cli.GetClient().DescribeZoneInfo(args.ID)
    91  		if e != nil {
    92  			return e
    93  		}
    94  		printObject(szone)
    95  		return nil
    96  	})
    97  
    98  	type PrivateZoneAddVpcOptions struct {
    99  		PRIVATEZONEID string
   100  		VPC           string
   101  		REGION        string
   102  	}
   103  	shellutils.R(&PrivateZoneAddVpcOptions{}, "privatezone-add-vpc", "associate vpc with privatezone", func(cli *aliyun.SRegion, args *PrivateZoneAddVpcOptions) error {
   104  		vpc := cloudprovider.SPrivateZoneVpc{}
   105  		vpc.Id = args.VPC
   106  		vpc.RegionId = args.REGION
   107  		err := cli.GetClient().BindZoneVpc(args.PRIVATEZONEID, &vpc)
   108  		if err != nil {
   109  			return err
   110  		}
   111  		return nil
   112  	})
   113  
   114  	type PrivateZoneDeleteVpcsOptions struct {
   115  		PRIVATEZONEID string
   116  	}
   117  	shellutils.R(&PrivateZoneDeleteVpcsOptions{}, "privatezone-delete-vpc", "delete vpc with privatezone", func(cli *aliyun.SRegion, args *PrivateZoneDeleteVpcsOptions) error {
   118  		err := cli.GetClient().UnBindZoneVpcs(args.PRIVATEZONEID)
   119  		if err != nil {
   120  			return err
   121  		}
   122  		return nil
   123  	})
   124  
   125  	type PrivateZoneRecordListOptions struct {
   126  		ID         string `help:"ID or name of privatezone"`
   127  		PageSize   int    `help:"page size"`
   128  		PageNumber int    `help:"page PageNumber"`
   129  	}
   130  	shellutils.R(&PrivateZoneRecordListOptions{}, "privatezonerecord-list", "List privatezonerecord", func(cli *aliyun.SRegion, args *PrivateZoneRecordListOptions) error {
   131  		srecords, e := cli.GetClient().DescribeZoneRecords(args.ID, args.PageNumber, args.PageSize)
   132  		if e != nil {
   133  			return e
   134  		}
   135  		printList(srecords.Records.Record, srecords.TotalItems, args.PageNumber, args.PageSize, []string{})
   136  		return nil
   137  	})
   138  
   139  	type PvtzRecordCreateOptions struct {
   140  		PRIVATEZONEID string `help:"PRIVATEZONEID"`
   141  		NAME          string `help:"Domain name"`
   142  		VALUE         string `help:"dns record value"`
   143  		TTL           int64  `help:"ttl"`
   144  		TYPE          string `help:"dns type"`
   145  		PolicyType    string `help:"PolicyType"`
   146  		Identify      string `help:"Identify"`
   147  	}
   148  	shellutils.R(&PvtzRecordCreateOptions{}, "privatezonerecord-create", "create privatezonerecord", func(cli *aliyun.SRegion, args *PvtzRecordCreateOptions) error {
   149  		opts := cloudprovider.DnsRecordSet{}
   150  		opts.DnsName = args.NAME
   151  		opts.DnsType = cloudprovider.TDnsType(args.TYPE)
   152  		opts.DnsValue = args.VALUE
   153  		opts.Ttl = args.TTL
   154  		opts.ExternalId = args.Identify
   155  		_, err := cli.GetClient().AddZoneRecord(args.PRIVATEZONEID, opts)
   156  		if err != nil {
   157  			return err
   158  		}
   159  		return nil
   160  	})
   161  
   162  	type PvtzRecordupdateOptions struct {
   163  		PRIVATEZONEID string `help:"PRIVATEZONEID"`
   164  		NAME          string `help:"Domain name"`
   165  		VALUE         string `help:"dns record value"`
   166  		TTL           int64  `help:"ttl"`
   167  		TYPE          string `help:"dns type"`
   168  		Identify      string `help:"Identify"`
   169  	}
   170  	shellutils.R(&PvtzRecordupdateOptions{}, "privatezonerecord-update", "update privatezonerecord", func(cli *aliyun.SRegion, args *PvtzRecordupdateOptions) error {
   171  		opts := cloudprovider.DnsRecordSet{}
   172  		opts.DnsName = args.NAME
   173  		opts.DnsType = cloudprovider.TDnsType(args.TYPE)
   174  		opts.DnsValue = args.VALUE
   175  		opts.Ttl = args.TTL
   176  		opts.ExternalId = args.Identify
   177  		err := cli.GetClient().UpdateZoneRecord(opts)
   178  		if err != nil {
   179  			return err
   180  		}
   181  		return nil
   182  	})
   183  
   184  	type PvtzRecordDeleteOptions struct {
   185  		PRIVATEZONERECORDID string `help:"PRIVATEZONEID"`
   186  	}
   187  	shellutils.R(&PvtzRecordDeleteOptions{}, "privatezonerecord-delete", "delete privatezonerecord", func(cli *aliyun.SRegion, args *PvtzRecordDeleteOptions) error {
   188  		err := cli.GetClient().DeleteZoneRecord(args.PRIVATEZONERECORDID)
   189  		if err != nil {
   190  			return err
   191  		}
   192  		return nil
   193  	})
   194  
   195  	type PvtzRecordSetStatusOptions struct {
   196  		PRIVATEZONERECORDID string `help:"PRIVATEZONEID"`
   197  		STATUS              string `choices:"ENABLE|DISABLE"`
   198  	}
   199  	shellutils.R(&PvtzRecordSetStatusOptions{}, "privatezonerecord-setstatus", "set privatezonerecord status", func(cli *aliyun.SRegion, args *PvtzRecordSetStatusOptions) error {
   200  
   201  		err := cli.GetClient().SetZoneRecordStatus(args.PRIVATEZONERECORDID, args.STATUS)
   202  		if err != nil {
   203  			return err
   204  		}
   205  		return nil
   206  	})
   207  }