github.com/aavshr/aws-sdk-go@v1.41.3/example/aws/endpoints/enumEndpoints/enumEndpoints.go (about) 1 //go:build example 2 // +build example 3 4 package main 5 6 import ( 7 "flag" 8 "fmt" 9 "os" 10 11 "github.com/aavshr/aws-sdk-go/aws/endpoints" 12 ) 13 14 // Demostrates how the SDK's endpoints can be enumerated over to discover 15 // regions, services, and endpoints defined by the SDK's Regions and Endpoints 16 // metadata. 17 // 18 // Usage: 19 // -p=id partition id, e.g: aws 20 // -r=id region id, e.g: us-west-2 21 // -s=id service id, e.g: s3 22 // 23 // -partitions Lists all partitions. 24 // -regions Lists all regions in a partition. Requires partition ID. 25 // If service ID is also provided will show endpoints for a service. 26 // -services Lists all services in a partition. Requires partition ID. 27 // If region ID is also provided, will show services available in that region. 28 // 29 // Example: 30 // go run enumEndpoints.go -p aws -services -r us-west-2 31 // 32 // Output: 33 // Services with endpoint us-west-2 in aws: 34 // ... 35 func main() { 36 var partitionID, regionID, serviceID string 37 flag.StringVar(&partitionID, "p", "", "Partition ID") 38 flag.StringVar(®ionID, "r", "", "Region ID") 39 flag.StringVar(&serviceID, "s", "", "Service ID") 40 41 var cmdPartitions, cmdRegions, cmdServices bool 42 flag.BoolVar(&cmdPartitions, "partitions", false, "Lists partitions.") 43 flag.BoolVar(&cmdRegions, "regions", false, "Lists regions of a partition. Requires partition ID to be provided. Will filter by a service if '-s' is set.") 44 flag.BoolVar(&cmdServices, "services", false, "Lists services for a partition. Requires partition ID to be provided. Will filter by a region if '-r' is set.") 45 flag.Parse() 46 47 partitions := endpoints.DefaultResolver().(endpoints.EnumPartitions).Partitions() 48 49 if cmdPartitions { 50 printPartitions(partitions) 51 } 52 53 if !(cmdRegions || cmdServices) { 54 return 55 } 56 57 p, ok := findPartition(partitions, partitionID) 58 if !ok { 59 fmt.Fprintf(os.Stderr, "Partition %q not found", partitionID) 60 os.Exit(1) 61 } 62 63 if cmdRegions { 64 printRegions(p, serviceID) 65 } 66 67 if cmdServices { 68 printServices(p, regionID) 69 } 70 } 71 72 func printPartitions(ps []endpoints.Partition) { 73 fmt.Println("Partitions:") 74 for _, p := range ps { 75 fmt.Println(p.ID()) 76 } 77 } 78 79 func printRegions(p endpoints.Partition, serviceID string) { 80 if len(serviceID) != 0 { 81 s, ok := p.Services()[serviceID] 82 if !ok { 83 fmt.Fprintf(os.Stderr, "service %q does not exist in partition %q", serviceID, p.ID()) 84 os.Exit(1) 85 } 86 es := s.Endpoints() 87 fmt.Printf("Endpoints for %s in %s:\n", serviceID, p.ID()) 88 for _, e := range es { 89 r, _ := e.ResolveEndpoint() 90 fmt.Printf("%s: %s\n", e.ID(), r.URL) 91 } 92 93 } else { 94 rs := p.Regions() 95 fmt.Printf("Regions in %s:\n", p.ID()) 96 for _, r := range rs { 97 fmt.Println(r.ID()) 98 } 99 } 100 } 101 102 func printServices(p endpoints.Partition, endpointID string) { 103 ss := p.Services() 104 105 if len(endpointID) > 0 { 106 fmt.Printf("Services with endpoint %s in %s:\n", endpointID, p.ID()) 107 } else { 108 fmt.Printf("Services in %s:\n", p.ID()) 109 } 110 111 for id, s := range ss { 112 if _, ok := s.Endpoints()[endpointID]; !ok && len(endpointID) > 0 { 113 continue 114 } 115 fmt.Println(id) 116 } 117 } 118 119 func findPartition(ps []endpoints.Partition, partitionID string) (endpoints.Partition, bool) { 120 for _, p := range ps { 121 if p.ID() == partitionID { 122 return p, true 123 } 124 } 125 126 return endpoints.Partition{}, false 127 }