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); }