forked from Scope-IT/marksman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Broker.cs
139 lines (120 loc) · 6.08 KB
/
Broker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SnipeSharp;
using System.Diagnostics;
using SnipeSharp.Endpoints.Models;
using SnipeSharp.Endpoints.SearchFilters;
using SnipeSharp.Common;
using System.Net;
namespace Marksman
{
class Broker
{
public Broker()
{
}
public bool CheckConnection(NameValueCollection appSettings)
{
// This method might seem overly complicated for what it is doing (simply
// checking a connection to the Snipe-IT instance. However, there are a lot
// of different ways that the connection can fail (usually related to improperly
// set values in the config file).
// This method allows a set of specific, descriptive error messages to be passed
// showing exactly what kind of configuration problem needs to be fixed.
string uri = "";
string query = "users?limit=0";
string baseUri = appSettings["BaseURI"];
// Note: The program should be able to handle a BaseURI that has a trailing '/' or not.
if (baseUri.EndsWith("/")){
uri = baseUri + query;
} else
{
uri = baseUri + "/" + query;
}
HttpWebRequest request;
try
{
request = (HttpWebRequest)WebRequest.Create(uri);
request.Headers["Authorization"] = "Bearer " + appSettings["API"];
request.Accept = "application/json";
}
catch (System.NotSupportedException e)
{
Console.WriteLine(e);
Console.WriteLine("Please double-check the BaseURI key in your <appSettings>\nblock of the Marksman config file and ensure it points to your instance of Snipe-IT.");
return false;
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("HTTP 200: Connection to Snipe-IT instance succeded.");
return true;
} else {
Console.WriteLine("HTTP {0}", response.StatusCode);
Console.WriteLine("Unexpected HTTP response code, could not connect to Snipe-IT instance.");
return false;
}
} catch (WebException e)
{
HttpWebResponse r = (HttpWebResponse)e.Response;
if (r == null)
{
Console.WriteLine(e);
Console.WriteLine("Please double-check the BaseURI key in your <appSettings>\nblock of the Marksman config file and ensure it points to your instance of Snipe-IT.");
}
else if (r.StatusCode == HttpStatusCode.Unauthorized)
{
Console.WriteLine("HTTP 403: Unauthorized. Please check the API key value in your <appSettings>\nblock of the Marksman config file and ensure it has been set to a valid key.");
}
else if (r.StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine("HTTP 404: URL not found. Please double-check the BaseURI key in your <appSettings>\nblock of the Marksman config file and ensure it points to your instance of Snipe-IT.");
} else
{
Console.WriteLine("Unexpected error, could not connect to Snipe-IT instance.");
Console.WriteLine(e);
}
return false;
}
}
public List<IRequestResponse> SyncAll(SnipeItApi snipe, Asset currentAsset, Model currentModel, Manufacturer currentManufacturer,
Category currentCategory, Company currentCompany, StatusLabel currentStatusLabel, Location currentLocation)
{
// Returns a list of messages with return info.
// This could be broken down further
List<IRequestResponse> messages = new List<IRequestResponse>();
messages.Add(snipe.ManufacturerManager.Create(currentManufacturer));
SearchFilter manufacturerFilter = new SearchFilter(currentManufacturer.Name);
Manufacturer updatedManufacturer = snipe.ManufacturerManager.FindOne(manufacturerFilter);
messages.Add(snipe.CategoryManager.Create(currentCategory));
SearchFilter categoryFilter = new SearchFilter(currentCategory.Name);
Category updatedCategory = snipe.CategoryManager.FindOne(categoryFilter);
currentModel.Manufacturer = updatedManufacturer;
currentModel.Category = updatedCategory;
messages.Add(snipe.ModelManager.Create(currentModel));
SearchFilter modelFilter = new SearchFilter(currentModel.Name);
Model updatedModel = snipe.ModelManager.FindOne(modelFilter);
messages.Add(snipe.CompanyManager.Create(currentCompany));
SearchFilter companyFilter = new SearchFilter(currentCompany.Name);
Company updatedCompany = snipe.CompanyManager.FindOne(companyFilter);
messages.Add(snipe.StatusLabelManager.Create(currentStatusLabel));
SearchFilter statusLabelFilter = new SearchFilter(currentStatusLabel.Name);
StatusLabel updatedStatusLabel = snipe.StatusLabelManager.FindOne(statusLabelFilter);
messages.Add(snipe.LocationManager.Create(currentLocation));
SearchFilter locationFilter = new SearchFilter(currentLocation.Name);
Location updatedLocation = snipe.LocationManager.FindOne(locationFilter);
currentAsset.Model = updatedModel;
currentAsset.Company = updatedCompany;
currentAsset.StatusLabel = updatedStatusLabel;
currentAsset.Location = updatedLocation;
messages.Add(snipe.AssetManager.Create(currentAsset));
return messages;
}
}
}