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

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/IBM-Cloud/bluemix-go"
    11  	v2 "github.com/IBM-Cloud/bluemix-go/api/container/containerv2"
    12  	"github.com/IBM-Cloud/bluemix-go/api/satellite/satellitev1"
    13  	"github.com/IBM-Cloud/bluemix-go/session"
    14  	"github.com/IBM-Cloud/bluemix-go/trace"
    15  )
    16  
    17  const sourceName = "portworx-service"
    18  
    19  func main() {
    20  
    21  	c := new(bluemix.Config)
    22  
    23  	var locationID string
    24  	flag.StringVar(&locationID, "locationID", "", "Location ID")
    25  
    26  	flag.Parse()
    27  
    28  	trace.Logger = trace.NewLogger("true")
    29  	if locationID == "" {
    30  		flag.Usage()
    31  		os.Exit(1)
    32  	}
    33  
    34  	sess, err := session.New(c)
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	target := v2.ClusterTargetHeader{}
    39  
    40  	satelliteClient, err := satellitev1.New(sess)
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  	satSourceAPI := satelliteClient.Source()
    45  	satEndpointAPI := satelliteClient.Endpoint()
    46  
    47  	// 1. Create Endpoint Source.
    48  	createSourceReq := satellitev1.CreateSatelliteEndpointSourceRequest{
    49  		LocationID: locationID,
    50  		Type:       "service",
    51  		SourceName: sourceName,
    52  		Addresses:  []string{"10.0.0.0/12"},
    53  	}
    54  	createSourceResp, err := satSourceAPI.CreateSatelliteEndpointSource(createSourceReq, target)
    55  	if err != nil {
    56  		log.Fatal(err)
    57  	}
    58  
    59  	fmt.Printf("Create Satellite endpoint source response: %+v\n", createSourceResp)
    60  
    61  	// 2. Verify Endpoint Source is created
    62  	out, err := satSourceAPI.ListSatelliteEndpointSources(locationID, target)
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  	fmt.Printf("List of sources added to satellite location [%s]: %+v\n", locationID, out)
    67  
    68  	// 3. Update existing source
    69  	var sourceID string
    70  	for _, satSource := range out.Sources {
    71  		if strings.EqualFold(satSource.SourceName, sourceName) {
    72  			sourceID = satSource.SourceID
    73  		}
    74  	}
    75  	updateSatSourceReq := satellitev1.UpdateSatelliteEndpointSourceRequest{
    76  		SourceName: sourceName,
    77  		Addresses:  []string{"10.8.0.0/24"},
    78  	}
    79  	updatedSatSource, err := satSourceAPI.UpdateSatelliteEndpointSources(locationID, sourceID, updateSatSourceReq, target)
    80  	if err != nil {
    81  		log.Fatal(err)
    82  	}
    83  	fmt.Printf("Updated satellite source details are: %+v\n", updatedSatSource)
    84  
    85  	// 4. List existing endpoints
    86  	endpoints, err := satEndpointAPI.GetEndpoints(locationID, target)
    87  
    88  	if err != nil {
    89  		log.Fatal(err)
    90  	}
    91  
    92  	var openShiftAPIEndpointID string
    93  
    94  	for _, ep := range endpoints.Endpoint {
    95  		if strings.Contains(ep.DisplayName, "openshift-api") {
    96  			openShiftAPIEndpointID = ep.EndpointID
    97  		}
    98  	}
    99  
   100  	// 4. Add endpoint source created in step 1 to `openshift-api-xxx-xxxx` endpoint
   101  
   102  	endpointSources := satellitev1.EndpointSource{
   103  		SourceID: createSourceResp.SourceID,
   104  		Enabled:  true,
   105  	}
   106  
   107  	addSourcesToEndpointReq := satellitev1.AddSourcesRequest{
   108  		Sources: []satellitev1.EndpointSource{endpointSources},
   109  	}
   110  
   111  	err = satEndpointAPI.AddSourcesToEndpoint(locationID, openShiftAPIEndpointID, addSourcesToEndpointReq, target)
   112  
   113  	if err != nil {
   114  		log.Fatal(err)
   115  	}
   116  }