github.com/sacloud/iaas-api-go@v1.12.0/search/example_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     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 search_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  	"time"
    22  
    23  	"github.com/sacloud/iaas-api-go"
    24  	"github.com/sacloud/iaas-api-go/search"
    25  )
    26  
    27  func Example() {
    28  	// API Keys
    29  	token := os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
    30  	secret := os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")
    31  	zone := os.Getenv("SAKURACLOUD_ZONE")
    32  
    33  	// API Client
    34  	caller := iaas.NewClient(token, secret)
    35  	serverOp := iaas.NewArchiveOp(caller)
    36  
    37  	// ******************************************
    38  	// Find
    39  	// ******************************************
    40  
    41  	// 名称に"Example"を含むサーバを検索
    42  	condition := &iaas.FindCondition{
    43  		Filter: search.Filter{
    44  			search.Key("Name"): search.PartialMatch("Example"),
    45  		},
    46  	}
    47  	searched, err := serverOp.Find(context.Background(), zone, condition)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	fmt.Printf("searched: %#v", searched)
    53  
    54  	// 以下の条件で検索
    55  	//   - 名称に"test"と"example"を含む
    56  	//   - ゾーンが"is1a"または"is1b"
    57  	//   - 作成日時が1週間以上前
    58  	condition = &iaas.FindCondition{
    59  		Filter: search.Filter{
    60  			search.Key("Name"):                               search.AndEqual("test", "example"),
    61  			search.Key("Zone.Name"):                          search.OrEqual("is1a", "is1b"),
    62  			search.KeyWithOp("CreatedAt", search.OpLessThan): time.Now().Add(-7 * 24 * time.Hour),
    63  		},
    64  	}
    65  	searched, err = serverOp.Find(context.Background(), zone, condition)
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  
    70  	fmt.Printf("searched: %#v", searched)
    71  }