public static bool CreateTemplate(Template newItem, CloudStack.SDK.Client client) { try { var path = newItem.Url; var format = System.IO.Path.GetExtension(path); //if (format == ".zip" || format == ".bz2" || format == ".gz") //{ // path = path.Remove(path.Length - format.Length); // format = System.IO.Path.GetExtension(path); //} // strip '.' format = format.Remove(0, ".".Length); // TODO: deal with async nature of create, currently assume template will no longer be 'ready' CloudStack.SDK.APIRequest req = new CloudStack.SDK.APIRequest("registerTemplate"); req.Parameters.Add("displaytext", newItem.Description); req.Parameters.Add("format", format.ToUpperInvariant()); req.Parameters.Add("hypervisor", DT2.Properties.Settings.Default.CloudStackHypervisor); //config: what hypervisor type are we targetting? req.Parameters.Add("ispublic", "false"); req.Parameters.Add("name", newItem.Name); req.Parameters.Add("ostypeid", newItem.OsType); req.Parameters.Add("url", newItem.Url); req.Parameters.Add("zoneid", DT2.Properties.Settings.Default.CloudStackZoneId); // config var rply = client.SendRequest(req); logger.Info("Create template request complete" + rply.ToString()); // TODO: Deal upload fail scenarios var template = rply.Root.Element("template"); var templateId = template.Element("id").Value; CloudStack.SDK.APIRequest reqTags = new CloudStack.SDK.APIRequest("createTags"); reqTags.Parameters.Add("resourceids", templateId); reqTags.Parameters.Add("resourcetype", "template"); if (!String.IsNullOrEmpty(newItem.DesktopType) && newItem.DesktopType == PublishedDesktopType) { reqTags.Parameters.Add("tags[0].key", DIaaSServerImage); reqTags.Parameters.Add("tags[0].value", "true"); } else { reqTags.Parameters.Add("tags[0].key", DIaaSDesktopImage); reqTags.Parameters.Add("tags[0].value", "true"); } var rply2 = client.SendRequest(reqTags); } catch (Exception e) { logger.Error("Create template failed!", e); return(false); } logger.Info("Created template " + newItem.Name); return(true); }
public static bool DeleteTemplate(string id, CloudStack.SDK.Client client) { try { // TODO: deal with async nature of delete, currently assume template will no longer be 'ready' CloudStack.SDK.APIRequest req = new CloudStack.SDK.APIRequest("deleteTemplate"); req.Parameters.Add("id", id); client.SendRequest(req); return(false); } catch (Exception e) { String errMsg = "Exception on Deleting template " + id + ", message:" + e.Message + e.StackTrace; logger.Error(errMsg); } return(true); }
internal void TriggerBadApiUrlException() { try { string apiUrl = serviceUri.AbsoluteUri.Substring(0, serviceUri.AbsoluteUri.Length - 3); Client session = new CloudStack.SDK.Client(new Uri(apiUrl), apiKey, secretKey); logWriter("Test bad XML exception triggered by GUI URL" + apiUrl); session.StartVirtualMachine("invalid-machine-id"); System.Diagnostics.Debug.Fail("Test should have triggered CloudStackException"); } catch (CloudStackException cex) { logWriter(cex.Message); logWriter("Test PASSED"); } }
internal void TriggerBadApiCallException() { try { logWriter("Test API call with wrong parameters"); Client session = new CloudStack.SDK.Client(this.serviceUri, this.apiKey, this.secretKey); CreateVolumeRequest request = new CreateVolumeRequest() { DiskOfferingId = "diskOfferingFoo", Size = 16, Name = "test", ZoneId = "zonefoo" }; session.CreateVolume(request); System.Diagnostics.Debug.Fail("Test should have triggered CloudStackException"); } catch (CloudStackException cex) { logWriter(cex.Message); logWriter("Test PASSED"); } }
internal string DeployVirtualMachine() { string id = string.Empty; var session = new CloudStack.SDK.Client(serviceUri, apiKey, secretKey); try { DeployVirtualMachineRequest request = new DeployVirtualMachineRequest() { TemplateId = templateId, ServiceOfferingId = serviceOfferingId, DisplayName = "Test-Generated", ZoneId = zoneId, UserData = "my user data", StartVm = !deployStopped }; if (!string.IsNullOrEmpty(this.networkId)) { request.WithNetworkIds(networkId); } id = session.DeployVirtualMachine(request); logWriter("Deployed new VM, id " + id); } catch (System.Exception wex) { logWriter(wex.Message); System.Diagnostics.Debug.Fail("Not supposed to throw during deploy VM"); } return id; }
public static List <Template> GetTemplates(CloudStack.SDK.Client csClient, string templateid) { logger.Debug("Enumerate templates, filter according to tags"); // TODO: can we filter by tag? var req = new CloudStack.SDK.ListTemplatesRequest(); req.Parameters.Add("templatefilter", DT2.Properties.Settings.Default.templatefilter); List <Template> templates = new List <Template>(); if (!string.IsNullOrEmpty(templateid)) { logger.Debug("Enumerate a single template, " + templateid); req.Parameters.Add("id", templateid); } try { // Raw result from ListTemplates queried using Linq, used to obtain tag information var resp = csClient.SendRequest(req); var taggedTemplates = from template in resp.Root.Elements("template") where template.Elements("tags").Count() > 0 select template; var targetZone = DT2.Properties.Settings.Default.XenDesktopAvailabilityZone; var imagesInZone = from template in taggedTemplates where template.Elements("zonename").Any(x => x.Value == targetZone) select template; var desktopImagesInZone = from template in imagesInZone where template.Elements("tags").Any(x => x.Element("key").Value == DIaaSDesktopImage) select template; var serverImagesInZone = from template in imagesInZone where template.Elements("tags").Any(x => x.Element("key").Value == DIaaSServerImage) select template; ConvertTemplate(desktopImagesInZone, templates, VirtualDesktopType); ConvertTemplate(serverImagesInZone, templates, PublishedDesktopType); //foreach (var item in rply.Template) //{ // // Linq query:# // // 1. enumerate 'template' XML elements (from template in resp.Root.Elements("template") // // 2. filter out all elements save that with same name as template we are inspecteding (where template.Elements("name").First().Value == item.Name // // 3. from this set select items with "tags" element // // Look at XML Use tags to spot XD's templates, which we do not list for the user // var templateTags = from template in resp.Root.Elements("template") where template.Elements("name").First().Value == item.Name select template.Elements("tags"); // if (templateTags.First().Count() < 1) // { // var tmplt = new Template() { Name = item.Name, Description = item.DisplayText, Id = item.Id, Ready = item.Status.ToString(), OsType = item.OsTypeId }; // logger.Debug("Enumerated template " + item.ToString()); // templates.Add(tmplt); // } //} } catch (Exception e) { String errMsg = "Exception on Template indexing: " + e.Message + e.StackTrace; logger.Error(errMsg); } return(templates); }
public static List <Template> GetTemplates(CloudStack.SDK.Client csClient) { return(GetTemplates(csClient, null)); }