尝试使用perl和json自动化Rally用户创建

时间:2013-07-10 18:08:24

标签: perl rally

我们正在尝试使用perl和json自动创建用户。我在PUT命令上有这个错误:

<h1>Method Not Allowed</h1>
<p>The requested method PUT is not allowed for the URL /https://sandbox.rallydev.com/slm/webservice/1.43/user/create.js.</p>
</body></html>
',
                                    '_rc' => '405',

尝试POST给了我这个:

<h1>Not Found</h1>
<p>The requested URL /https://sandbox.rallydev.com/slm/webservice/1.43/user/create.js was not found on this server.</p>
</body></html>
',
                                    '_rc' => '404',

是否有使用perl创建Rally用户的示例?

2 个答案:

答案 0 :(得分:0)

您使用https错误地启动了您的URI及其/方案。删除它。

答案 1 :(得分:0)

这是Perl中的一个基本示例,展示了如何创建Rally用户。需要以下Perl模块:

  • REST客户端::
  • JS​​ON
  • MIME :: Base64编码

我在Ubuntu 12.04上使用v5.14.2对此进行了测试。

use strict;
use warnings;

# Needed includes
use REST::Client;
use JSON;

use MIME::Base64;
use URI;
use URI::Split;

# User parameters
# Must be a Rally Subscription Administrator, or a Workspace Administrator
# in a Rally Workspace whose Workspace Admins have been granted user administration
# privileges
my $username='user@company.com';
my $password='topsecret';

# Rally connection parameters
my $base_url="https://rally1.rallydev.com";
my $wsapi_version = "1.43";

# Connect to Rally and create user
# ====================================

my $headers = {
    Accept => 'application/json',
    Authorization => 'Basic ' . encode_base64($username . ':' . $password)    
};

# instantiate REST Client
my $rest_client = REST::Client->new();

# set host
$rest_client->setHost($base_url);

# Formulate POST request to create user
# Note that the a Rally User must have at least one Workspace Permission / Project Permission
# pair. Thus the user will be given User Permissions in the Default Workspace of the
# Subscription or Workspace Administrator whose RallyID is used to run this script.
# The user will receive Viewer-level permissions to the alphabetically
# First project in the Default Workspace of the Rally UserID used to run the script

my $new_rally_user_id = 'user12@company.com';

print "Connecting to Rally and attempting to create user of UserID: \n";
print $new_rally_user_id . "\n\n";

my $wsapi_endpoint = "/slm/webservice/" . $wsapi_version . "/";
my $user_create_endpoint = $wsapi_endpoint . "user/create.js";
my $user_post_body = '{"User": {"UserName": "' . $new_rally_user_id . '", "EmailAddress": "' . $new_rally_user_id . '"}}';

print "POST Body for user creation:\n";
print $user_post_body . "\n\n";

# Attempt Create
$rest_client->POST($user_create_endpoint, $user_post_body, $headers);

# Output JSON Response
print "Create Response:\n\n";
print $rest_client->responseContent();

以下是运行脚本的结果:

$ perl rally_create_user.pl 
Connecting to Rally and attempting to create user of UserID: 
user12@company.com

POST Body for user creation:
{"User": {"UserName": "user12@company.com", "EmailAddress": "user12@company.com"}}

Create Response:
{ "CreateResult" : { "Errors" : [  ],
  "Object" : { "Boolean" : null,
      "CUF" : null,
      "CostCenter" : "None",
      "CreationDate" : "2013-07-11T22:49:00.056Z",
      "Date" : null,
      "Department" : "None",
      "Disabled" : false,
      "DisplayName" : null,
      "Editable" : null,
      "EmailAddress" : "user12@company.com",
      "FirstName" : null,
      "LandingPage" : "/dashboard",
      "LastLoginDate" : null,
      "LastName" : null,
      "LastPasswordUpdateDate" : "2013-07-11T22:49:00.056Z",
      "MiddleName" : null,
      "NetworkID" : null,
      "ObjectID" : 12345678913,
      "OfficeLocation" : "None",
      "OnpremLdapUsername" : null,
      "Phone" : null,
      "RevisionHistory" : { "_rallyAPIMajor" : "1",
          "_rallyAPIMinor" : "43",
          "_ref" : "https://rally1.rallydev.com/slm/webservice/1.43/revisionhistory/12345678915.js",
          "_type" : "RevisionHistory"
        },
      "Role" : "None",
      "ShortDisplayName" : null,
      "Specialty" : null,
      "String" : null,
      "Subscription" : { "_rallyAPIMajor" : "1",
          "_rallyAPIMinor" : "43",
          "_ref" : "https://rally1.rallydev.com/slm/webservice/1.43/subscription/12345678914.js",
          "_refObjectName" : "My Subscription",
          "_type" : "Subscription"
        },
      "SubscriptionAdmin" : false,
      "TeamMemberships" : [  ],
      "Text" : null,
      "UserName" : "user12@company.com",
      "UserPermissions" : [ { "_rallyAPIMajor" : "1",
            "_rallyAPIMinor" : "43",
            "_ref" : "https://rally1.rallydev.com/slm/webservice/1.43/workspacepermission/12345678917u12345678918w3.js",
            "_refObjectName" : "My Workspace User",
            "_type" : "WorkspacePermission"
          },
          { "_rallyAPIMajor" : "1",
            "_rallyAPIMinor" : "43",
            "_ref" : "https://rally1.rallydev.com/slm/webservice/1.43/projectpermission/12345678919u12345678920p1.js",
            "_refObjectName" : "A Project",
            "_type" : "ProjectPermission"
          }
        ],
      "UserProfile" : { "_rallyAPIMajor" : "1",
          "_rallyAPIMinor" : "43",
          "_ref" : "https://rally1.rallydev.com/slm/webservice/1.43/userprofile/12345678921.js",
          "_type" : "UserProfile"
        },
      "_CreatedAt" : "just now",
      "_objectVersion" : "2",
      "_rallyAPIMajor" : "1",
      "_rallyAPIMinor" : "43",
      "_ref" : "https://rally1.rallydev.com/slm/webservice/1.43/user/12345678922.js",
      "_refObjectName" : "user12",
      "_type" : "User",
      "yesno" : null
    },
  "Warnings" : [ "API status is Deprecated and will become Not Supported on 2014-Jun-20" ],
  "_rallyAPIMajor" : "1",
  "_rallyAPIMinor" : "43"