github.com/coreos/mantle@v0.13.0/platform/api/azure/replication.go (about) 1 // Copyright 2016 CoreOS, Inc. 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 azure 16 17 import ( 18 "encoding/xml" 19 "fmt" 20 21 "github.com/Azure/azure-sdk-for-go/management/location" 22 ) 23 24 const ( 25 computeService = "Compute" 26 ) 27 28 var ( 29 azureImageReplicateURL = "services/images/%s/replicate" 30 azureImageUnreplicateURL = "services/images/%s/unreplicate" 31 ) 32 33 type ReplicationInput struct { 34 XMLName xml.Name `xml:"http://schemas.microsoft.com/windowsazure ReplicationInput"` 35 TargetLocations []string `xml:"TargetLocations>Region"` 36 Offer string `xml:"ComputeImageAttributes>Offer"` 37 Sku string `xml:"ComputeImageAttributes>Sku"` 38 Version string `xml:"ComputeImageAttributes>Version"` 39 } 40 41 // Locations returns a slice of Azure Locations which offer the Compute 42 // service, useful for replicating to all Locations. 43 func (a *API) Locations() ([]string, error) { 44 lc := location.NewClient(a.client) 45 46 llr, err := lc.ListLocations() 47 if err != nil { 48 return nil, err 49 } 50 51 var locations []string 52 53 for _, l := range llr.Locations { 54 haveCompute := false 55 for _, svc := range l.AvailableServices { 56 if svc == computeService { 57 haveCompute = true 58 break 59 } 60 } 61 62 if haveCompute { 63 locations = append(locations, l.Name) 64 } else { 65 plog.Infof("Skipping location %q without %s service", l.Name, computeService) 66 } 67 } 68 69 return locations, nil 70 } 71 72 func (a *API) ReplicateImage(image, offer, sku, version string, regions ...string) error { 73 ri := ReplicationInput{ 74 TargetLocations: regions, 75 Offer: offer, 76 Sku: sku, 77 Version: version, 78 } 79 80 data, err := xml.Marshal(&ri) 81 if err != nil { 82 return err 83 } 84 85 url := fmt.Sprintf(azureImageReplicateURL, image) 86 87 op, err := a.client.SendAzurePutRequest(url, "", data) 88 if err != nil { 89 return err 90 } 91 92 return a.client.WaitForOperation(op, nil) 93 } 94 95 func (a *API) UnreplicateImage(image string) error { 96 url := fmt.Sprintf(azureImageUnreplicateURL, image) 97 op, err := a.client.SendAzurePutRequest(url, "", []byte{}) 98 if err != nil { 99 return err 100 } 101 102 return a.client.WaitForOperation(op, nil) 103 }