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

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/IBM-Cloud/bluemix-go/api/mccp/mccpv2"
     9  	"github.com/IBM-Cloud/bluemix-go/session"
    10  	"github.com/IBM-Cloud/bluemix-go/trace"
    11  )
    12  
    13  func main() {
    14  	var org string
    15  	flag.StringVar(&org, "org", "", "Bluemix Organization")
    16  
    17  	var spacequota string
    18  	flag.StringVar(&spacequota, "spacequota", "", "Bluemix Space Quota Definition")
    19  
    20  	var newspacequota string
    21  	flag.StringVar(&newspacequota, "newspacequota", "", "Bluemix Space Quota Definition")
    22  
    23  	flag.Parse()
    24  
    25  	if org == "" || spacequota == "" || newspacequota == "" {
    26  		flag.Usage()
    27  		os.Exit(1)
    28  	}
    29  
    30  	trace.Logger = trace.NewLogger("true")
    31  	sess, err := session.New()
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  
    36  	client, err := mccpv2.New(sess)
    37  
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  
    42  	region := sess.Config.Region
    43  	orgAPI := client.Organizations()
    44  	myorg, err := orgAPI.FindByName(org, region)
    45  
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  
    50  	createRequest := mccpv2.SpaceQuotaCreateRequest{
    51  		Name:                    spacequota,
    52  		OrgGUID:                 myorg.GUID,
    53  		MemoryLimitInMB:         1024,
    54  		InstanceMemoryLimitInMB: 1024,
    55  		RoutesLimit:             50,
    56  		ServicesLimit:           150,
    57  		NonBasicServicesAllowed: false,
    58  	}
    59  
    60  	spaceQuotaAPI := client.SpaceQuotas()
    61  	_, err = spaceQuotaAPI.Create(createRequest)
    62  
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  
    67  	quota, err := spaceQuotaAPI.FindByName(spacequota, myorg.GUID)
    68  
    69  	if err != nil {
    70  		log.Fatal(err)
    71  	}
    72  
    73  	updateRequest := mccpv2.SpaceQuotaUpdateRequest{
    74  		Name: newspacequota,
    75  	}
    76  
    77  	_, err = spaceQuotaAPI.Update(updateRequest, quota.GUID)
    78  
    79  	if err != nil {
    80  		log.Fatal(err)
    81  	}
    82  
    83  	quota, err = spaceQuotaAPI.FindByName(newspacequota, myorg.GUID)
    84  
    85  	if err != nil {
    86  		log.Fatal(err)
    87  	}
    88  
    89  	log.Println(quota.GUID, myorg.GUID)
    90  
    91  	err = spaceQuotaAPI.Delete(quota.GUID)
    92  
    93  	if err != nil {
    94  		log.Fatal(err)
    95  	}
    96  }