yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/shell/dns_domain.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  // PageNumberations 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 DnsProductListOptions struct {
    39  		PageNumber int `help:"page size" default:"1"`
    40  		PageSize   int `help:"page PageSize" default:"20"`
    41  	}
    42  	shellutils.R(&DnsProductListOptions{}, "dnsproduct-list", "List dnsproduct", func(cli *aliyun.SRegion, args *DnsProductListOptions) error {
    43  		//products, e := cli.GetClient().DescribeDnsProductInstances(args.PageNumber, args.PageSize)
    44  		products, e := cli.GetClient().GetAllDnsProductInstances()
    45  		if e != nil {
    46  			return e
    47  		}
    48  		//printList(products.DNSProducts.DNSProduct, products.TotalCount, args.PageSize, args.PageNumber, []string{})
    49  		printList(products, len(products), args.PageNumber, args.PageSize, []string{})
    50  		return nil
    51  	})
    52  
    53  	type DomianListOptions struct {
    54  		PageNumber int `help:"page size" default:"1"`
    55  		PageSize   int `help:"page PageSize" default:"20"`
    56  	}
    57  	shellutils.R(&DomianListOptions{}, "domain-list", "List Domain", func(cli *aliyun.SRegion, args *DomianListOptions) error {
    58  		sdomains, e := cli.GetClient().DescribeDomains(args.PageNumber, args.PageSize)
    59  		if e != nil {
    60  			return e
    61  		}
    62  		printList(sdomains.Domains.Domain, sdomains.TotalCount, args.PageNumber, args.PageSize, []string{})
    63  		return nil
    64  	})
    65  
    66  	type SDomainCreateOptions struct {
    67  		DOMAINNAME string `help:"Domain name"`
    68  	}
    69  	shellutils.R(&SDomainCreateOptions{}, "domain-create", "Create domain", func(cli *aliyun.SRegion, args *SDomainCreateOptions) error {
    70  		hostzones, err := cli.GetClient().AddDomain(args.DOMAINNAME)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		printObject(hostzones)
    75  		return nil
    76  	})
    77  
    78  	type SDomainDeleteOptions struct {
    79  		DOMAINNAME string
    80  	}
    81  	shellutils.R(&SDomainDeleteOptions{}, "domain-delete", "delete domain", func(cli *aliyun.SRegion, args *SDomainDeleteOptions) error {
    82  		err := cli.GetClient().DeleteDomain(args.DOMAINNAME)
    83  		if err != nil {
    84  			return err
    85  		}
    86  		return nil
    87  	})
    88  
    89  	type SDomainShowOptions struct {
    90  		DOMAINNAME string
    91  	}
    92  	shellutils.R(&SDomainShowOptions{}, "domain-show", "Show domain", func(cli *aliyun.SRegion, args *SDomainShowOptions) error {
    93  		szone, e := cli.GetClient().DescribeDomainInfo(args.DOMAINNAME)
    94  		if e != nil {
    95  			return e
    96  		}
    97  		printObject(szone)
    98  		return nil
    99  	})
   100  
   101  	type SDomainRecordListOptions struct {
   102  		DOMAINNAME string
   103  		PageNumber int `help:"page size" default:"1"`
   104  		PageSize   int `help:"page PageSize" default:"20"`
   105  	}
   106  	shellutils.R(&SDomainRecordListOptions{}, "domainrecord-list", "List domainrecord", func(cli *aliyun.SRegion, args *SDomainRecordListOptions) error {
   107  		srecords, e := cli.GetClient().DescribeDomainRecords(args.DOMAINNAME, args.PageNumber, args.PageSize)
   108  		if e != nil {
   109  			return e
   110  		}
   111  		printList(srecords.DomainRecords.Record, srecords.TotalCount, args.PageNumber, args.PageSize, []string{})
   112  		return nil
   113  	})
   114  
   115  	type DomainRecordCreateOptions struct {
   116  		DOMAINNAME  string
   117  		NAME        string
   118  		VALUE       string `help:"dns record value"`
   119  		TTL         int64  `help:"ttl"`
   120  		TYPE        string `help:"dns type"`
   121  		PolicyType  string `help:"PolicyType"`
   122  		PolicyValue string
   123  	}
   124  	shellutils.R(&DomainRecordCreateOptions{}, "domainrecord-create", "create domainrecord", func(cli *aliyun.SRegion, args *DomainRecordCreateOptions) error {
   125  		opts := cloudprovider.DnsRecordSet{}
   126  		opts.DnsName = args.NAME
   127  		opts.DnsType = cloudprovider.TDnsType(args.TYPE)
   128  		opts.DnsValue = args.VALUE
   129  		opts.Ttl = args.TTL
   130  		opts.PolicyType = cloudprovider.TDnsPolicyType(args.PolicyType)
   131  
   132  		_, err := cli.GetClient().AddDomainRecord(args.DOMAINNAME, opts)
   133  		if err != nil {
   134  			return err
   135  		}
   136  		return nil
   137  	})
   138  
   139  	type DomainRecordupdateOptions struct {
   140  		DOMAINRECORDID string `help:"DOMAINRECORDID"`
   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  	}
   147  	shellutils.R(&DomainRecordupdateOptions{}, "domainrecord-update", "update domainrecord", func(cli *aliyun.SRegion, args *DomainRecordupdateOptions) error {
   148  		opts := cloudprovider.DnsRecordSet{}
   149  		opts.DnsName = args.NAME
   150  		opts.DnsType = cloudprovider.TDnsType(args.TYPE)
   151  		opts.DnsValue = args.VALUE
   152  		opts.Ttl = args.TTL
   153  		opts.ExternalId = args.DOMAINRECORDID
   154  		opts.PolicyType = cloudprovider.TDnsPolicyType(args.PolicyType)
   155  		err := cli.GetClient().UpdateDomainRecord(opts)
   156  		if err != nil {
   157  			return err
   158  		}
   159  		return nil
   160  	})
   161  
   162  	type DomainRecordDeleteOptions struct {
   163  		DOMAINRECORDID string `help:"DOMAINRECORDID"`
   164  	}
   165  	shellutils.R(&DomainRecordDeleteOptions{}, "domainrecord-delete", "delete domainrecord", func(cli *aliyun.SRegion, args *DomainRecordDeleteOptions) error {
   166  		err := cli.GetClient().DeleteDomainRecord(args.DOMAINRECORDID)
   167  		if err != nil {
   168  			return err
   169  		}
   170  		return nil
   171  	})
   172  
   173  	type DomainRecordSetStatusOptions struct {
   174  		DOMAINRECORDID string `help:"PRIVATEZONEID"`
   175  		STATUS         string `choices:"Enable|Disable"`
   176  	}
   177  	shellutils.R(&DomainRecordSetStatusOptions{}, "domainrecord-setstatus", "set domainrecord status", func(cli *aliyun.SRegion, args *DomainRecordSetStatusOptions) error {
   178  
   179  		err := cli.GetClient().SetDomainRecordStatus(args.DOMAINRECORDID, args.STATUS)
   180  		if err != nil {
   181  			return err
   182  		}
   183  		return nil
   184  	})
   185  }