-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement2Payload.java
147 lines (124 loc) · 6.01 KB
/
implement2Payload.java
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
140
141
142
143
144
145
146
147
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.simple.JSONObject;
public class implement2Payload {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("CSV file name is missing.");
return;
}
String csvFilePath = args[0];
try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {
String headerLine = br.readLine();
if (headerLine == null) {
System.out.println("CSV file is empty.");
return;
}
String[] header = headerLine.split(",");
String line;
int row = 1;
while ((line = br.readLine()) != null) {
String[] values = line.split(",", -1); // Split the line and include empty values
JSONObject jsonPayload = createJsonPayload(header, values);
System.out.println("Batch " + row);
System.out.println(jsonPayload);
System.out.println();
executeCurlCommand(jsonPayload);
row++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static JSONObject createJsonPayload(String[] header, String[] values) {
ArrayList<JSONObject> connectionProperties = new ArrayList<>();
ArrayList<JSONObject> securityProperties = new ArrayList<>();
connectionProperties.add(createConnectionProperty(header, values));
securityProperties.add(createSecurityProperty(header, values));
securityProperties.add(createPasswordProperty(header, values));
HashMap<String, Object> jsonPayload = new HashMap<>();
jsonPayload.put("connectionProperties", connectionProperties);
jsonPayload.put("securityPolicy", "BASIC_AUTH");
jsonPayload.put("securityProperties", securityProperties);
return new JSONObject(jsonPayload);
}
private static JSONObject createConnectionProperty(String[] header, String[] values) {
HashMap<String, Object> connectionProperty = new HashMap<>();
connectionProperty.put("displayName", "Connection URL");
connectionProperty.put("hasAttachment", false);
connectionProperty.put("hiddenFlag", false);
connectionProperty.put("propertyGroup", "CONNECTION_PROPS");
connectionProperty.put("propertyName", "connectionUrl");
connectionProperty.put("propertyShortDesc", "Please make sure that this value really corresponds to the type selected above.");
connectionProperty.put("propertyType", "URL");
connectionProperty.put("propertyValue", getValueForHeader(header, values, "ConnectionURL"));
connectionProperty.put("requiredFlag", true);
return new JSONObject(connectionProperty);
}
private static JSONObject createSecurityProperty(String[] header, String[] values) {
HashMap<String, Object> securityProperty = new HashMap<>();
securityProperty.put("displayName", "Username");
securityProperty.put("hasAttachment", false);
securityProperty.put("hiddenFlag", false);
securityProperty.put("propertyDescription", "A username credential");
securityProperty.put("propertyGroup", "CREDENTIALS");
securityProperty.put("propertyName", "username");
securityProperty.put("propertyType", "STRING");
securityProperty.put("propertyValue", getValueForHeader(header, values, "Username"));
securityProperty.put("requiredFlag", true);
return new JSONObject(securityProperty);
}
private static JSONObject createPasswordProperty(String[] header, String[] values) {
HashMap<String, Object> passwordProperty = new HashMap<>();
passwordProperty.put("displayName", "Password");
passwordProperty.put("hasAttachment", false);
passwordProperty.put("hiddenFlag", false);
passwordProperty.put("propertyDescription", "A password credential");
passwordProperty.put("propertyGroup", "CREDENTIALS");
passwordProperty.put("propertyName", "password");
passwordProperty.put("propertyType", "PASSWORD");
passwordProperty.put("propertyValue", getValueForHeader(header, values, "Password"));
passwordProperty.put("requiredFlag", true);
return new JSONObject(passwordProperty);
}
private static String getValueForHeader(String[] header, String[] values, String headerName) {
int index = getIndex(header, headerName);
if (index >= 0 && index < values.length) {
return values[index];
}
return "";
}
private static int getIndex(String[] array, String value) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(value)) {
return i;
}
}
return -1;
}
private static void executeCurlCommand(JSONObject jsonPayload) {
String authorization = "${Devops_user}:${Oic_Jenkins#2023}";
String endpoint = "https://testinstance-idevjxz332qf-ia.integration.ocp.oraclecloud.com/ic/api/integration/v1/connections/NEWRCON";
String curlCommand = String.format(
"curl --header \"Authorization: Basic %s\" --header \"X-HTTP-Method-Override: PATCH\" --header \"Content-Type: application/json\" -d '%s' %s",
authorization,
jsonPayload.toString(),
endpoint
);
try {
ProcessBuilder processBuilder = new ProcessBuilder(curlCommand.split(" "));
Process process = processBuilder.start();
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("CURL command executed successfully.");
} else {
System.out.println("CURL command execution failed with exit code: " + exitCode);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}