Skip to content

Commit 04f1fa5

Browse files
committed
Refactors code to fix usings
Adds support for importing domains Fixes an issue where setting the maxMessageBytes would throw an exception (issue #4)
1 parent 37ea10a commit 04f1fa5

36 files changed

+724
-570
lines changed

src/Lithnet.GoogleApps.MA.Setup/Product.wxs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Product Id="*"
44
Name="Lithnet GoogleApps Management Agent"
55
Language="1033"
6-
Version="1.1.6086"
6+
Version="1.1.6088"
77
Manufacturer="Lithnet"
88
UpgradeCode="3410d571b358426281edb2990ae57cae" >
99

src/Lithnet.GoogleApps.MA.UnitTests/Lithnet.GoogleApps.MA.UnitTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@
8585
<HintPath>..\packages\Google.GData.Extensions.2.2.0.0\lib\Google.GData.Extensions.dll</HintPath>
8686
<Private>True</Private>
8787
</Reference>
88-
<Reference Include="Lithnet.GoogleApps, Version=1.0.6086.35275, Culture=neutral, processorArchitecture=MSIL">
89-
<HintPath>..\packages\Lithnet.GoogleApps.1.0.6086.35275\lib\net452\Lithnet.GoogleApps.dll</HintPath>
88+
<Reference Include="Lithnet.GoogleApps, Version=1.0.6089.23020, Culture=neutral, processorArchitecture=MSIL">
89+
<HintPath>..\packages\Lithnet.GoogleApps.1.0.6089.23020\lib\net452\Lithnet.GoogleApps.dll</HintPath>
9090
<Private>True</Private>
9191
</Reference>
9292
<Reference Include="Lithnet.Logging, Version=1.0.5774.20685, Culture=neutral, processorArchitecture=MSIL">

src/Lithnet.GoogleApps.MA.UnitTests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<package id="Google.GData.Client" version="2.2.0.0" targetFramework="net452" />
1111
<package id="Google.GData.Contacts" version="2.2.0.0" targetFramework="net452" />
1212
<package id="Google.GData.Extensions" version="2.2.0.0" targetFramework="net452" />
13-
<package id="Lithnet.GoogleApps" version="1.0.6086.35275" targetFramework="net452" />
13+
<package id="Lithnet.GoogleApps" version="1.0.6089.23020" targetFramework="net452" />
1414
<package id="Lithnet.Logging" version="1.0.5774.20685" targetFramework="net452" />
1515
<package id="Lithnet.MetadirectoryServices" version="1.0.6017.24789" targetFramework="net452" />
1616
<package id="log4net" version="2.0.5" targetFramework="net452" />

src/Lithnet.GoogleApps.MA/ApiInterfaces/ApiInterfaceAdvancedUser.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
2+
using Lithnet.GoogleApps.ManagedObjects;
3+
using Lithnet.MetadirectoryServices;
4+
using Microsoft.MetadirectoryServices;
65

76
namespace Lithnet.GoogleApps.MA
87
{
9-
using ManagedObjects;
10-
using MetadirectoryServices;
11-
using Microsoft.MetadirectoryServices;
12-
138
internal class ApiInterfaceAdvancedUser : ApiInterfaceUser
149
{
1510
public ApiInterfaceAdvancedUser(MASchemaType type)
1611
:base (type)
1712
{
1813
this.InternalInterfaces.Add(new ApiInterfaceUserDelegates());
19-
this.objectClass = SchemaConstants.AdvancedUser;
14+
this.ObjectClass = SchemaConstants.AdvancedUser;
2015
}
2116

2217
public override object CreateInstance(CSEntryChange csentry)

src/Lithnet.GoogleApps.MA/ApiInterfaces/ApiInterfaceContact.cs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
4-
using System.Text;
5+
using System.Text.RegularExpressions;
56
using System.Threading.Tasks;
7+
using Lithnet.Logging;
8+
using System.Diagnostics.CodeAnalysis;
9+
using Google.GData.Contacts;
10+
using Google.GData.Extensions;
11+
using Lithnet.MetadirectoryServices;
12+
using Microsoft.MetadirectoryServices;
613

714
namespace Lithnet.GoogleApps.MA
815
{
9-
using System.Diagnostics.CodeAnalysis;
10-
using Google.Contacts;
11-
using Google.GData.Contacts;
12-
using Google.GData.Extensions;
13-
using MetadirectoryServices;
14-
using Microsoft.MetadirectoryServices;
15-
1616
internal class ApiInterfaceContact : IApiInterfaceObject
1717
{
1818
internal const string DNAttributeName = "lithnet-google-ma-dn";
@@ -22,7 +22,7 @@ internal class ApiInterfaceContact : IApiInterfaceObject
2222

2323
private string domain;
2424

25-
protected MASchemaType SchemaType { get; set; }
25+
protected MASchemaType SchemaType { get; set; }
2626

2727
static ApiInterfaceContact()
2828
{
@@ -198,6 +198,53 @@ public string GetDNValue(object target)
198198
return contactEntry.PrimaryEmail == null ? null : "contact:" + contactEntry.PrimaryEmail.Address;
199199
}
200200

201+
public Task GetItems(IManagementAgentParameters config, Schema schema, BlockingCollection<object> collection)
202+
{
203+
Task t = new Task(() =>
204+
{
205+
Logger.WriteLine("Starting contacts import task");
206+
207+
HashSet<string> seenDNs = new HashSet<string>();
208+
209+
foreach (ContactEntry contact in ContactRequestFactory.GetContacts(config.Domain))
210+
{
211+
if (!string.IsNullOrWhiteSpace(config.ContactRegexFilter))
212+
{
213+
if (contact.PrimaryEmail != null)
214+
{
215+
if (!Regex.IsMatch(contact.PrimaryEmail.Address, config.ContactRegexFilter, RegexOptions.IgnoreCase))
216+
{
217+
continue;
218+
}
219+
}
220+
}
221+
222+
string dn = this.GetDNValue(contact);
223+
224+
if (dn == null)
225+
{
226+
Logger.WriteLine($"Contact {contact.SelfUri.Content} had no DN or primary email attribute, ignoring");
227+
continue;
228+
}
229+
230+
if (!seenDNs.Add(dn))
231+
{
232+
Logger.WriteLine($"Ignoring contact {contact.SelfUri.Content} with duplicate dn {dn}");
233+
continue;
234+
}
235+
236+
237+
collection.Add(ImportProcessor.GetCSEntryChange(contact, schema.Types[SchemaConstants.Contact]));
238+
}
239+
240+
Logger.WriteLine("Contacts import task complete");
241+
});
242+
243+
t.Start();
244+
245+
return t;
246+
}
247+
201248
public bool SetDNValue(CSEntryChange csentry, ContactEntry e)
202249
{
203250
if (csentry.ObjectModificationType == ObjectModificationType.Add)
@@ -232,7 +279,7 @@ public bool SetDNValue(CSEntryChange csentry, ContactEntry e)
232279

233280
if (dn == null)
234281
{
235-
dn = new ExtendedProperty {Name = ApiInterfaceContact.DNAttributeName};
282+
dn = new ExtendedProperty { Name = ApiInterfaceContact.DNAttributeName };
236283
e.ExtendedProperties.Add(dn);
237284
}
238285

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using System.Diagnostics.CodeAnalysis;
7+
using Google.GData.Contacts;
8+
using Lithnet.GoogleApps.ManagedObjects;
9+
using Lithnet.Logging;
10+
using Lithnet.MetadirectoryServices;
11+
using Microsoft.MetadirectoryServices;
12+
13+
namespace Lithnet.GoogleApps.MA
14+
{
15+
internal class ApiInterfaceDomain : IApiInterfaceObject
16+
{
17+
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
18+
private static ApiInterfaceKeyedCollection internalInterfaces;
19+
20+
protected MASchemaType SchemaType { get; set; }
21+
22+
private string customerID;
23+
24+
static ApiInterfaceDomain()
25+
{
26+
ApiInterfaceDomain.internalInterfaces = new ApiInterfaceKeyedCollection();
27+
}
28+
29+
public ApiInterfaceDomain(string customerID, MASchemaType type)
30+
{
31+
this.SchemaType = type;
32+
this.customerID = customerID;
33+
}
34+
35+
public string Api => "domain";
36+
37+
38+
public object CreateInstance(CSEntryChange csentry)
39+
{
40+
return new ContactEntry();
41+
}
42+
43+
public object GetInstance(CSEntryChange csentry)
44+
{
45+
string id = csentry.GetAnchorValueOrDefault<string>("domainName");
46+
47+
if (id == null)
48+
{
49+
throw new AttributeNotPresentException("domainName");
50+
}
51+
52+
return DomainsRequestFactory.Get(this.customerID, id);
53+
}
54+
55+
public void DeleteInstance(CSEntryChange csentry)
56+
{
57+
string id = csentry.GetAnchorValueOrDefault<string>("domainName");
58+
59+
if (id == null)
60+
{
61+
throw new AttributeNotPresentException("domainName");
62+
}
63+
64+
DomainsRequestFactory.Delete(this.customerID, id);
65+
}
66+
67+
public IList<AttributeChange> ApplyChanges(CSEntryChange csentry, SchemaType type, ref object target, bool patch = false)
68+
{
69+
bool hasChanged = false;
70+
List<AttributeChange> changes = new List<AttributeChange>();
71+
Domain obj = (Domain)target;
72+
73+
if (this.SetDNValue(csentry, obj))
74+
{
75+
hasChanged = true;
76+
}
77+
78+
foreach (IAttributeAdapter typeDef in this.SchemaType.Attributes.Where(t => t.Api == this.Api))
79+
{
80+
if (typeDef.UpdateField(csentry, obj))
81+
{
82+
hasChanged = true;
83+
}
84+
}
85+
86+
if (hasChanged)
87+
{
88+
Domain result;
89+
90+
if (csentry.ObjectModificationType == ObjectModificationType.Add)
91+
{
92+
result = DomainsRequestFactory.Insert(this.customerID, obj);
93+
target = result;
94+
}
95+
else if (csentry.ObjectModificationType == ObjectModificationType.Replace || csentry.ObjectModificationType == ObjectModificationType.Update)
96+
{
97+
throw new InvalidOperationException("Domain objects are read only");
98+
}
99+
else
100+
{
101+
throw new InvalidOperationException();
102+
}
103+
104+
changes.AddRange(this.GetLocalChanges(csentry.DN, csentry.ObjectModificationType, type, result));
105+
}
106+
107+
foreach (IApiInterface i in ApiInterfaceDomain.internalInterfaces)
108+
{
109+
changes.AddRange(i.ApplyChanges(csentry, type, ref target, patch));
110+
}
111+
112+
return changes;
113+
}
114+
115+
public IList<AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
116+
{
117+
List<AttributeChange> attributeChanges = this.GetLocalChanges(dn, modType, type, source);
118+
119+
foreach (IApiInterface i in ApiInterfaceDomain.internalInterfaces)
120+
{
121+
attributeChanges.AddRange(i.GetChanges(dn, modType, type, source));
122+
}
123+
124+
return attributeChanges;
125+
}
126+
127+
private List<AttributeChange> GetLocalChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
128+
{
129+
List<AttributeChange> attributeChanges = new List<AttributeChange>();
130+
131+
Domain entry = source as Domain;
132+
133+
if (entry == null)
134+
{
135+
throw new InvalidOperationException();
136+
}
137+
138+
foreach (IAttributeAdapter typeDef in this.SchemaType.Attributes.Where(t => t.Api == this.Api))
139+
{
140+
if (typeDef.IsAnchor)
141+
{
142+
continue;
143+
}
144+
145+
foreach (AttributeChange change in typeDef.CreateAttributeChanges(dn, modType, entry))
146+
{
147+
if (type.HasAttribute(change.Name))
148+
{
149+
attributeChanges.Add(change);
150+
}
151+
}
152+
}
153+
return attributeChanges;
154+
}
155+
156+
public string GetAnchorValue(object target)
157+
{
158+
Domain d = target as Domain;
159+
160+
if (d != null)
161+
{
162+
return d.DomainName;
163+
}
164+
165+
throw new InvalidOperationException();
166+
}
167+
168+
public string GetDNValue(object target)
169+
{
170+
Domain d = target as Domain;
171+
172+
if (d == null)
173+
{
174+
throw new InvalidOperationException();
175+
}
176+
177+
return d.DomainName;
178+
}
179+
180+
public Task GetItems(IManagementAgentParameters config, Schema schema, BlockingCollection<object> collection)
181+
{
182+
Task t = new Task(() =>
183+
{
184+
Logger.WriteLine("Starting domains import task");
185+
186+
DomainList list = DomainsRequestFactory.List(config.CustomerID);
187+
188+
foreach (Domain d in list.Domains)
189+
{
190+
string dn = this.GetDNValue(d);
191+
192+
if (dn == null)
193+
{
194+
Logger.WriteLine($"Domain {d} had no DN, ignoring");
195+
continue;
196+
}
197+
198+
collection.Add(ImportProcessor.GetCSEntryChange(d, schema.Types[SchemaConstants.Domain]));
199+
}
200+
201+
Logger.WriteLine("Domains import task complete");
202+
});
203+
204+
t.Start();
205+
206+
return t;
207+
}
208+
209+
public bool SetDNValue(CSEntryChange csentry, Domain e)
210+
{
211+
if (csentry.ObjectModificationType == ObjectModificationType.Add)
212+
{
213+
e.DomainName = csentry.DN;
214+
return true;
215+
}
216+
else
217+
{
218+
throw new NotSupportedException($"The DN value is read only and cannot be changed");
219+
}
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)