1 Star 0 Fork 0

zhuchance/kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
results.go 2.75 KB
一键复制 编辑 原始数据 按行查看 历史
Masahiro Sano 提交于 2015-04-18 18:35 . Update gophercloud to latest
package cloudnetworks
import (
"fmt"
"reflect"
"time"
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// CloudNetwork represents a network associated with a RackConnect configuration.
type CloudNetwork struct {
// Specifies the ID of the newtork.
ID string `mapstructure:"id"`
// Specifies the user-provided name of the network.
Name string `mapstructure:"name"`
// Specifies the IP range for this network.
CIDR string `mapstructure:"cidr"`
// Specifies the time the network was created.
CreatedAt time.Time `mapstructure:"-"`
// Specifies the time the network was last updated.
UpdatedAt time.Time `mapstructure:"-"`
}
// CloudNetworkPage is the page returned by a pager when traversing over a
// collection of CloudNetworks.
type CloudNetworkPage struct {
pagination.SinglePageBase
}
// IsEmpty returns true if a CloudNetworkPage contains no CloudNetworks.
func (r CloudNetworkPage) IsEmpty() (bool, error) {
cns, err := ExtractCloudNetworks(r)
if err != nil {
return true, err
}
return len(cns) == 0, nil
}
// ExtractCloudNetworks extracts and returns CloudNetworks. It is used while iterating over
// a cloudnetworks.List call.
func ExtractCloudNetworks(page pagination.Page) ([]CloudNetwork, error) {
var res []CloudNetwork
casted := page.(CloudNetworkPage).Body
err := mapstructure.Decode(casted, &res)
var rawNets []interface{}
switch casted.(type) {
case interface{}:
rawNets = casted.([]interface{})
default:
return res, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
}
for i := range rawNets {
thisNet := (rawNets[i]).(map[string]interface{})
if t, ok := thisNet["created"].(string); ok && t != "" {
creationTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].CreatedAt = creationTime
}
if t, ok := thisNet["updated"].(string); ok && t != "" {
updatedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return res, err
}
res[i].UpdatedAt = updatedTime
}
}
return res, err
}
// GetResult represents the result of a Get operation.
type GetResult struct {
gophercloud.Result
}
// Extract is a function that extracts a CloudNetwork from a GetResult.
func (r GetResult) Extract() (*CloudNetwork, error) {
if r.Err != nil {
return nil, r.Err
}
var res CloudNetwork
err := mapstructure.Decode(r.Body, &res)
b := r.Body.(map[string]interface{})
if date, ok := b["created"]; ok && date != nil {
t, err := time.Parse(time.RFC3339, date.(string))
if err != nil {
return nil, err
}
res.CreatedAt = t
}
if date, ok := b["updated"]; ok && date != nil {
t, err := time.Parse(time.RFC3339, date.(string))
if err != nil {
return nil, err
}
res.UpdatedAt = t
}
return &res, err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v0.20.1

搜索帮助

D67c1975 1850385 1daf7b77 1850385