Skip to content

Commit 8268dcd

Browse files
committed
Add generated code for Gallery and Tags libraries
1 parent e8c0f1a commit 8268dcd

25 files changed

+2908
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using Microsoft.Azure.Commands.Common.Authentication;
19+
using Microsoft.Azure.Commands.Common.Authentication.Models;
20+
using Microsoft.Azure.Management.Internal.Resources;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
23+
24+
namespace Microsoft.Azure.Commands.ResourceManager.Common.Tags
25+
{
26+
public class TagsClient
27+
{
28+
public const string ExecludedTagPrefix = "hidden-related:/";
29+
30+
public IResourceManagementClient ResourceManagementClient { get; set; }
31+
32+
public Action<string> VerboseLogger { get; set; }
33+
34+
public Action<string> ErrorLogger { get; set; }
35+
36+
/// <summary>
37+
/// Creates new tags client instance.
38+
/// </summary>
39+
/// <param name="context">The Azure context instance</param>
40+
public TagsClient(IAzureContext context)
41+
: this(AzureSession.Instance.ClientFactory.CreateArmClient<ResourceManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager))
42+
{
43+
44+
}
45+
46+
/// <summary>
47+
/// Creates new TagsClient instance
48+
/// </summary>
49+
/// <param name="resourceManagementClient">The IResourceManagementClient instance</param>
50+
public TagsClient(IResourceManagementClient resourceManagementClient)
51+
{
52+
ResourceManagementClient = resourceManagementClient;
53+
}
54+
55+
/// <summary>
56+
/// Parameterless constructor for mocking
57+
/// </summary>
58+
public TagsClient()
59+
{
60+
61+
}
62+
63+
public List<PSTag> ListTags()
64+
{
65+
var result = ResourceManagementClient.Tags.List();
66+
List<PSTag> tags = new List<PSTag>();
67+
68+
do
69+
{
70+
result.Where(t => !t.TagName.StartsWith(ExecludedTagPrefix)).ForEach(t => tags.Add(t.ToPSTag()));
71+
72+
if (!string.IsNullOrEmpty(result.NextPageLink))
73+
{
74+
result = ResourceManagementClient.Tags.ListNext(result.NextPageLink);
75+
}
76+
} while (!string.IsNullOrEmpty(result.NextPageLink));
77+
78+
return tags;
79+
}
80+
81+
public PSTag GetTag(string tag)
82+
{
83+
List<PSTag> tags = ListTags();
84+
if (!tags.Exists(t => t.Name.Equals(tag, StringComparison.OrdinalIgnoreCase)))
85+
{
86+
throw new Exception(string.Format(Properties.Resources.TagNotFoundMessage, tag));
87+
}
88+
89+
return tags.First(t => t.Name.Equals(tag, StringComparison.OrdinalIgnoreCase));
90+
}
91+
92+
/// <summary>
93+
/// Creates a tag and if the tag name exists add the value to the existing tag name.
94+
/// </summary>
95+
/// <param name="tag">The tag name</param>
96+
/// <param name="values">The tag values</param>
97+
/// <returns>The tag object</returns>
98+
public PSTag CreateTag(string tag, List<string> values)
99+
{
100+
ResourceManagementClient.Tags.CreateOrUpdate(tag);
101+
102+
if (values != null)
103+
{
104+
values.ForEach(v => ResourceManagementClient.Tags.CreateOrUpdateValue(tag, v));
105+
}
106+
107+
return GetTag(tag);
108+
}
109+
110+
/// <summary>
111+
/// Deletes the entire tag or specific tag value.
112+
/// </summary>
113+
/// <param name="tag">The tag name</param>
114+
/// <param name="values">Values to remove</param>
115+
/// <returns></returns>
116+
public PSTag DeleteTag(string tag, List<string> values)
117+
{
118+
PSTag tagObject = null;
119+
120+
121+
if (values == null || values.Count != 1)
122+
{
123+
tagObject = GetTag(tag);
124+
if (int.Parse(tagObject.Count) > 0)
125+
{
126+
throw new Exception(Properties.Resources.CanNotDeleteTag);
127+
}
128+
}
129+
130+
if (values == null || values.Count == 0)
131+
{
132+
tagObject = GetTag(tag);
133+
tagObject.Values.ForEach(v => ResourceManagementClient.Tags.DeleteValue(tag, v.Name));
134+
ResourceManagementClient.Tags.Delete(tag);
135+
}
136+
else
137+
{
138+
values.ForEach(v => ResourceManagementClient.Tags.DeleteValue(tag, v));
139+
tagObject = GetTag(tag);
140+
}
141+
142+
return tagObject;
143+
}
144+
}
145+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using Microsoft.Azure.Management.Internal.Resources.Models;
20+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
21+
22+
namespace Microsoft.Azure.Commands.ResourceManager.Common.Tags
23+
{
24+
public static class TagsExtensions
25+
{
26+
public static PSTag ToPSTag(this TagDetails tag)
27+
{
28+
return new PSTag()
29+
{
30+
Count = tag.Count.Value.ToString(),
31+
Name = tag.TagName,
32+
Values = tag.Values.Select(v => v.ToPSTagValue()).ToList(),
33+
ValuesTable = ConstructTagValuesTable(tag.Values.ToList())
34+
};
35+
}
36+
37+
public static PSTagValue ToPSTagValue(this TagValue value)
38+
{
39+
return new PSTagValue()
40+
{
41+
Count = value.Count.Value.ToString(),
42+
Name = value.TagValueProperty
43+
};
44+
}
45+
46+
private static TagValue EmptyTagValue
47+
{
48+
get
49+
{
50+
return new TagValue()
51+
{
52+
TagValueProperty = string.Empty,
53+
Id = string.Empty,
54+
Count = new TagCount()
55+
{
56+
Type = string.Empty
57+
}
58+
};
59+
}
60+
}
61+
62+
private static string ConstructTagValuesTable(List<TagValue> tagValues)
63+
{
64+
StringBuilder tagValuesTable = new StringBuilder();
65+
66+
if (tagValues.Count > 0)
67+
{
68+
int maxNameLength = Math.Max("Name".Length, tagValues.Where(v => v.TagValueProperty != null).DefaultIfEmpty(EmptyTagValue).Max(v => v.TagValueProperty.Length));
69+
int maxCountLength = Math.Max("Count".Length, tagValues.Where(v => v.Count.Value != null).DefaultIfEmpty(EmptyTagValue).Max(v => v.Count.Value.ToString().Length));
70+
71+
string rowFormat = "{0, -" + maxNameLength + "} {1, -" + maxCountLength + "}\r\n";
72+
tagValuesTable.AppendLine();
73+
tagValuesTable.AppendFormat(rowFormat, "Name", "Count");
74+
tagValuesTable.AppendFormat(rowFormat,
75+
GeneralUtilities.GenerateSeparator(maxNameLength, "="),
76+
GeneralUtilities.GenerateSeparator(maxCountLength, "="));
77+
78+
foreach (TagValue tagValue in tagValues)
79+
{
80+
tagValuesTable.AppendFormat(rowFormat, tagValue.TagValueProperty, tagValue.Count.Value);
81+
}
82+
}
83+
84+
return tagValuesTable.ToString();
85+
}
86+
}
87+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Newtonsoft.Json;
16+
17+
namespace Microsoft.Azure.Management.Internal.Resources.Utilities.Models
18+
{
19+
public class DeploymentVariable
20+
{
21+
[JsonProperty("type")]
22+
public string Type { get; set; }
23+
24+
[JsonProperty("value")]
25+
public object Value { get; set; }
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Management.Internal.Resources.Utilities.Models
16+
{
17+
public class FilterResourcesOptions
18+
{
19+
public string Name { get; set; }
20+
21+
public string ResourceGroup { get; set; }
22+
23+
public string ResourceType { get; set; }
24+
25+
public FilterResourcesOptions()
26+
{
27+
Name = null;
28+
ResourceGroup = null;
29+
ResourceType = null;
30+
}
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Newtonsoft.Json;
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
using System.Threading.Tasks;
21+
22+
namespace Microsoft.Azure.Management.Internal.Resources.Models
23+
{
24+
public partial class GenericResource
25+
{
26+
/// <summary>
27+
/// Gets or sets the resource group.
28+
/// </summary>
29+
[JsonProperty(PropertyName = "resourceGroupName")]
30+
public string ResourceGroupName { get; set; }
31+
}
32+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Management.Internal.Resources.Models;
16+
using System;
17+
using System.Collections.Generic;
18+
19+
namespace Microsoft.Azure.Management.Internal.Resources.Utilities.Models
20+
{
21+
public class ResourceGroupDeployment
22+
{
23+
public string DeploymentName { get; set; }
24+
25+
public string CorrelationId { get; set; }
26+
27+
public string ResourceGroupName { get; set; }
28+
29+
public string ProvisioningState { get; set; }
30+
31+
public DateTime? Timestamp { get; set; }
32+
33+
public DeploymentMode? Mode { get; set; }
34+
35+
public TemplateLink TemplateLink { get; set; }
36+
37+
public string TemplateLinkString { get; set; }
38+
39+
public string DeploymentDebugLogLevel { get; set; }
40+
41+
public Dictionary<string, DeploymentVariable> Parameters { get; set; }
42+
43+
public string ParametersString
44+
{
45+
get { return ResourcesExtensions.ConstructDeploymentVariableTable(Parameters); }
46+
}
47+
48+
public Dictionary<string, DeploymentVariable> Outputs { get; set; }
49+
50+
public string OutputsString
51+
{
52+
get { return ResourcesExtensions.ConstructDeploymentVariableTable(Outputs); }
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)