github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/examples/icd/icdv4/whitelist/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"log"
     6  	"net/url"
     7  	"os"
     8  
     9  	"github.com/IBM-Cloud/bluemix-go/api/icd/icdv4"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  	"github.com/IBM-Cloud/bluemix-go/trace"
    12  )
    13  
    14  func main() {
    15  
    16  	var icdId string
    17  	var ipAddress string
    18  	var description string
    19  	flag.StringVar(&icdId, "icdId", "", "CRN of the IBM Cloud Database service instance")
    20  	flag.StringVar(&ipAddress, "ipAddress", "", "IpAddress in CIDR notation")
    21  	flag.StringVar(&description, "description", "", "IP address description")
    22  	flag.Parse()
    23  
    24  	if icdId == "" {
    25  		flag.Usage()
    26  		os.Exit(1)
    27  	}
    28  
    29  	icdId = url.PathEscape(icdId)
    30  	trace.Logger = trace.NewLogger("true")
    31  	sess, err := session.New()
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  
    36  	icdClient, err := icdv4.New(sess)
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  	whitelistAPI := icdClient.Whitelists()
    41  	taskAPI := icdClient.Tasks()
    42  
    43  	whitelistReq := icdv4.WhitelistReq{
    44  		WhitelistEntry: icdv4.WhitelistEntry{
    45  			Address:     ipAddress,
    46  			Description: description,
    47  		},
    48  	}
    49  	task, err := whitelistAPI.CreateWhitelist(icdId, whitelistReq)
    50  	log.Println(">>>>>>>> Task 1 Create Hitelist :", task)
    51  
    52  	count := 0
    53  	for {
    54  		innerTask, err := taskAPI.GetTask(task.Id)
    55  		if err != nil {
    56  			log.Fatal(err)
    57  		}
    58  		count = count + 1
    59  		log.Printf("Task : %v     %v\n", count, innerTask)
    60  		if innerTask.Status != "running" {
    61  			break
    62  		}
    63  	}
    64  
    65  	whitelist, err := whitelistAPI.GetWhitelist(icdId)
    66  
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	log.Println("Whitelist :", whitelist)
    71  
    72  	task, err = whitelistAPI.DeleteWhitelist(icdId, ipAddress)
    73  	log.Println(">>>>>>>> Task 2 Delete Whitelist :", task)
    74  
    75  	count = 0
    76  	for {
    77  		innerTask, err := taskAPI.GetTask(task.Id)
    78  		if err != nil {
    79  			log.Fatal(err)
    80  		}
    81  		count = count + 1
    82  		log.Printf("Task : %v     %v\n", count, innerTask)
    83  		// Querying status after completion returns ''. So 'completed' or '' as completed
    84  		if innerTask.Status != "running" {
    85  			break
    86  		}
    87  	}
    88  
    89  }