Profile Login IP Ranges via API
I haven't done this in Python, but I have in Java - here's some sample code:
// Set up MetadataConnection from an EnterpriseConnection as in
// http://www.salesforce.com/us/developer/docs/api_meta/Content/meta_quickstart_java_sample.htm
// Ranges should be like this
// String[][] ranges = {
// { "1.0.0.0", "1.255.255.255" },
// { "3.0.0.0", "3.255.255.255" }
// };
public static void setLoginIpRanges(MetadataConnection metadataConnection,
String profileName, String[][] ranges) throws ConnectionException {
private static long longestWaitMilliSecs = 16000; // Read from config, whatever
try {
ProfileLoginIpRange[] loginIpRanges = new ProfileLoginIpRange[ranges.length];
for (int i = 0; i < ranges.length; i++) {
loginIpRanges[i] = new ProfileLoginIpRange();
loginIpRanges[i].setStartAddress(ranges[i][0]);
loginIpRanges[i].setEndAddress(ranges[i][1]);
}
Profile profile = new Profile();
profile.setLoginIpRanges(loginIpRanges);
profile.setFullName(profileName);
UpdateMetadata updateMetadata = new UpdateMetadata();
updateMetadata.setMetadata(profile);
updateMetadata.setCurrentName(profileName);
AsyncResult[] ars = metadataConnection
.update(new UpdateMetadata[] { updateMetadata });
AsyncResult asyncResult = ars[0];
long waitTimeMilliSecs = 1000;
while (!asyncResult.isDone() && waitTimeMilliSecs <= longestWaitMilliSecs) {
Thread.sleep(waitTimeMilliSecs);
waitTimeMilliSecs *= 2;
asyncResult = metadataConnection
.checkStatus(new String[] { asyncResult.getId() })[0];
System.out.println("Status is: " + asyncResult.getState());
}
if (asyncResult.getState() != AsyncRequestState.Completed) {
System.out.println(asyncResult.getStatusCode() + " msg: "
+ asyncResult.getMessage());
}
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (ConnectionException ce) {
ce.printStackTrace();
}
}
I have recently automated whitelisting the Login Ip range in profiles through metadata API, here is my code:
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.metadata.FileProperties;
import com.sforce.soap.metadata.ListMetadataQuery;
import com.sforce.soap.metadata.Metadata;
import com.sforce.soap.metadata.MetadataConnection;
import com.sforce.soap.metadata.Profile;
import com.sforce.soap.metadata.ProfileLoginIpRange;
import com.sforce.ws.ConnectionException;
public class MetadataSample {
private MetadataConnection metadataconnection;
private void run() throws ConnectionException, Exception
{
login();
String[][]ranges = {{"1.2.3.0","1.2.3.255"}, { "2.3.4.0", "2.3.4.255" } };
ListMetadataQuery query = new ListMetadataQuery();
query.setType("Profile");
double asOfVersion = 39.;
FileProperties[] lmr = metadataconnection.listMetadata(new ListMetadataQuery[] {query}, asOfVersion);
if (lmr != null)
{
for (FileProperties n : lmr) {
System.out.println("Component fullName: " + n.getFullName());
setLoginIpRanges(n.getFullName(),ranges);
}}
// setLoginIpRanges("Standard",ranges);
}
public static void main(String[] args) throws Exception
{
MetadataSample samples1 = new MetadataSample();
samples1.run();
}
private void login() throws ConnectionException
{
String username = System.getenv("");
String password = System.getenv("");
ConnectorConfig config = new ConnectorConfig();
config.setUsername(username);
config.setPassword(password);
EnterpriseConnection connection;
connection = Connector.newConnection(config);
ConnectorConfig metadataConfig = new ConnectorConfig();
metadataConfig.setSessionId(connection.getSessionHeader().getSessionId());
metadataconnection = com.sforce.soap.metadata.Connector.newConnection(metadataConfig);
}
private void setLoginIpRanges(String string,String[][] ranges) throws ConnectionException, InterruptedException
{
ProfileLoginIpRange[] loginIpRanges = new ProfileLoginIpRange[ranges.length];
for (int i =0;i<ranges.length; i++)
{
loginIpRanges[i] = new ProfileLoginIpRange();
loginIpRanges[i].setStartAddress(ranges[i][0]);
loginIpRanges[i].setEndAddress(ranges[i][1]);
}
Profile profile = new Profile();
profile.setLoginIpRanges(loginIpRanges);
profile.setFullName(string);
com.sforce.soap.metadata.SaveResult[] results = metadataconnection.updateMetadata( new Metadata[] {profile});
for (com.sforce.soap.metadata.SaveResult r : results)
{
if (r.isSuccess())
{
System.out.println("Updated profile: " + r.getFullName());
} else
{
System.out.println("Errors were encountered while creating " + r.getFullName());
for (com.sforce.soap.metadata.Error e : r.getErrors())
{
System.out.println("Error message: " + e.getMessage());
System.out.println("Status code: " + e.getStatusCode());
}
}
}
}
}