yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/host.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 openstack
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/pkg/errors"
    21  )
    22  
    23  type SHost struct {
    24  	HostName string
    25  	Zone     string
    26  	Service  string
    27  }
    28  
    29  type SAggregate struct {
    30  	AvailabilityZone string
    31  	CreatedAt        time.Time
    32  	Deleted          bool
    33  	Hosts            []string
    34  	Id               string
    35  	Metadata         map[string]string
    36  	Name             string
    37  	Uuid             string
    38  }
    39  
    40  func (region *SRegion) GetAggregates() ([]SAggregate, error) {
    41  	resp, err := region.ecsList("/os-aggregates", nil)
    42  	if err != nil {
    43  		return nil, errors.Wrap(err, "ecsList")
    44  	}
    45  	aggregates := []SAggregate{}
    46  	err = resp.Unmarshal(&aggregates, "aggregates")
    47  	if err != nil {
    48  		return nil, errors.Wrap(err, `resp.Unmarshal(&aggregates, "aggregates")`)
    49  	}
    50  	return aggregates, nil
    51  }
    52  
    53  func (region *SRegion) GetHosts() ([]SHost, error) {
    54  	hosts := []SHost{}
    55  	resp, err := region.ecsList("/os-hosts", nil)
    56  	if err != nil {
    57  		return nil, errors.Wrap(err, "ecsList(os-hosts)")
    58  	}
    59  	err = resp.Unmarshal(&hosts, "hosts")
    60  	if err != nil {
    61  		return nil, errors.Wrap(err, "resp.Unmarshal")
    62  	}
    63  	return hosts, nil
    64  }